Last active
August 21, 2021 05:50
-
-
Save foundrium/9105687 to your computer and use it in GitHub Desktop.
Play Framework Unit Testing Examples
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# This is the main configuration file for the application. | |
# ~~~~~ | |
# Secret key | |
# ~~~~~ | |
# The secret key is used to secure cryptographics functions. | |
# If you deploy your application to several instances be sure to use the same key! | |
application.secret="9=HEdH[JB<nsLe0^eqL:h4Lxc^_AQPNC<m8WOU7qGt6]t5Q?;P:wnVtZWT@Y9J?4" | |
# The application languages | |
# ~~~~~ | |
application.langs="en" | |
# Global object class | |
# ~~~~~ | |
# Define the Global object class for this application. | |
# Default to Global in the root package. | |
# application.global=Global | |
# Router | |
# ~~~~~ | |
# Define the Router object to use for this application. | |
# This router will be looked up first when the application is starting up, | |
# so make sure this is the entry point. | |
# Furthermore, it's assumed your route file is named properly. | |
# So for an application router like `conf/my.application.Router`, | |
# you may need to define a router file `my.application.routes`. | |
# Default to Routes in the root package (and `conf/routes`) | |
# application.router=my.application.Routes | |
# Database configuration | |
# ~~~~~ | |
# You can declare as many datasources as you want. | |
# By convention, the default datasource is named `default` | |
# | |
# db.default.driver=org.h2.Driver | |
# db.default.url="jdbc:h2:mem:play" | |
# db.default.user=sa | |
# db.default.password="" | |
# | |
# You can expose this datasource via JNDI if needed (Useful for JPA) | |
# db.default.jndiName=DefaultDS | |
# Evolutions | |
# ~~~~~ | |
# You can disable evolutions if needed | |
# evolutionplugin=disabled | |
# Ebean configuration | |
# ~~~~~ | |
# You can declare as many Ebean servers as you want. | |
# By convention, the default server is named `default` | |
# | |
# ebean.default="models.*" | |
# Logger | |
# ~~~~~ | |
# You can also configure logback (http://logback.qos.ch/), | |
# by providing an application-logger.xml file in the conf directory. | |
# Root logger: | |
logger.root=ERROR | |
# Logger used by the framework: | |
logger.play=INFO | |
# Logger provided to your application: | |
logger.application=DEBUG | |
# RabbitMQ configuration | |
rabbitmq.host=localhost | |
rabbitmq.queue=queue1 | |
rabbitmq.exchange=exchange1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import static org.fest.assertions.Assertions.assertThat; | |
import static play.mvc.Http.Status.CREATED; | |
import static play.test.Helpers.contentAsString; | |
import static play.test.Helpers.contentType; | |
import static play.test.Helpers.fakeApplication; | |
import static play.test.Helpers.route; | |
import static play.test.Helpers.running; | |
import static play.test.Helpers.status; | |
import java.io.IOException; | |
import java.util.UUID; | |
import models.Notification; | |
import models.NotificationType; | |
import org.junit.Assert; | |
import org.junit.Before; | |
import org.junit.Test; | |
import play.mvc.Result; | |
import play.test.FakeRequest; | |
import com.fasterxml.jackson.core.JsonProcessingException; | |
import com.fasterxml.jackson.databind.JsonNode; | |
import com.fasterxml.jackson.databind.ObjectMapper; | |
import controllers.NotificationController; | |
public class ApplicationTest { | |
private Notification _notification; | |
private JsonNode _notificationJsonNode; | |
@Before | |
public void setup() { | |
ObjectMapper mapper = new ObjectMapper(); | |
String json; | |
_notification = new Notification(UUID.randomUUID(), | |
new NotificationType(UUID.randomUUID(), "Critical"), | |
"This is a test message"); | |
try { | |
json = mapper.writeValueAsString(_notification); | |
_notificationJsonNode = mapper.readTree(json); | |
} catch (JsonProcessingException e) { | |
e.printStackTrace(); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
} | |
@Test | |
public void addMessageToQueue() { | |
final GlobalUnitTest settings = new GlobalUnitTest(); | |
running(fakeApplication(settings), new Runnable() { | |
public void run() { | |
// Arrange | |
FakeRequest request = new FakeRequest("POST", "/api/v1/notifications"); | |
request.withJsonBody(_notificationJsonNode); | |
// Act | |
Result result = route(request); | |
// Assert | |
assertThat(status(result)).isEqualTo(CREATED); | |
assertThat(contentType(result)).isEqualTo("application/json"); | |
assertThat(contentAsString(result)).contains("This is a test message"); | |
try { | |
NotificationController notificationController = settings.getControllerInstance(NotificationController.class); | |
assertThat(notificationController.QueueService.count()).isEqualTo(1); | |
} catch (Exception e) { | |
Assert.fail(); | |
} | |
} | |
}); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name := "notifications-push" | |
version := "1.0-SNAPSHOT" | |
libraryDependencies ++= Seq( | |
javaJdbc, | |
javaEbean, | |
cache, | |
"com.rabbitmq" % "amqp-client" % "3.2.3", | |
"com.google.inject" % "guice" % "3.0" | |
) | |
play.Project.playJavaSettings |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.google.inject.AbstractModule; | |
import com.google.inject.Guice; | |
import com.google.inject.Injector; | |
import play.Application; | |
import play.GlobalSettings; | |
import services.IQueueService; | |
import services.RabbitQueueService; | |
public class Global extends GlobalSettings { | |
private Injector injector; | |
@Override | |
public void onStart(Application application) { | |
injector = Guice.createInjector(new AbstractModule() { | |
@Override | |
protected void configure() { | |
bind(IQueueService.class).to(RabbitQueueService.class); | |
} | |
}); | |
} | |
@Override | |
public <T> T getControllerInstance(Class<T> aClass) throws Exception { | |
return injector.getInstance(aClass); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.google.inject.AbstractModule; | |
import com.google.inject.Guice; | |
import com.google.inject.Injector; | |
import play.Application; | |
import play.GlobalSettings; | |
import services.IQueueService; | |
import services.FakeQueueService; | |
public class GlobalUnitTest extends GlobalSettings { | |
private Injector injector; | |
@Override | |
public void onStart(Application application) { | |
injector = Guice.createInjector(new AbstractModule() { | |
@Override | |
protected void configure() { | |
bind(IQueueService.class).to(FakeQueueService.class); | |
} | |
}); | |
} | |
@Override | |
public <T> T getControllerInstance(Class<T> aClass) throws Exception { | |
return injector.getInstance(aClass); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package controllers; | |
import java.io.IOException; | |
import models.Notification; | |
import play.libs.Json; | |
import play.mvc.BodyParser; | |
import play.mvc.Controller; | |
import play.mvc.Result; | |
import play.mvc.Results; | |
import services.IQueueService; | |
import utils.Config; | |
import com.fasterxml.jackson.core.JsonParseException; | |
import com.fasterxml.jackson.databind.JsonMappingException; | |
import com.fasterxml.jackson.databind.JsonNode; | |
import com.fasterxml.jackson.databind.ObjectMapper; | |
import com.google.inject.Inject; | |
import com.google.inject.Singleton; | |
import com.rabbitmq.client.Channel; | |
import com.rabbitmq.client.Connection; | |
import com.rabbitmq.client.ConnectionFactory; | |
@Singleton | |
public class NotificationController extends Controller { | |
public IQueueService QueueService; | |
@Inject | |
public NotificationController(IQueueService service) { | |
QueueService = service; | |
} | |
@BodyParser.Of(BodyParser.Json.class) | |
public Result post() { | |
ObjectMapper mapper = new ObjectMapper(); | |
Notification notification; | |
try { | |
JsonNode notificationJsonNode = Controller.request().body().asJson(); | |
notification = mapper.readValue(notificationJsonNode.toString(), | |
Notification.class); | |
QueueService.push(notification); | |
return Results.created(notificationJsonNode, "UTF-8"); | |
} catch (JsonParseException e) { | |
e.printStackTrace(); | |
} catch (JsonMappingException e) { | |
e.printStackTrace(); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
return Results.badRequest(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Routes | |
# This file defines all application routes (Higher priority routes first) | |
# ~~~~ | |
# Home page | |
GET / controllers.Application.index() | |
# Map static resources from the /public folder to the /assets URL path | |
GET /assets/*file controllers.Assets.at(path="/public", file) | |
# API resources | |
POST /api/v1/notifications @controllers.NotificationController.post() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment