From f8e0ef75d3b0bddcd6c497f6994915830e4eb6c7 Mon Sep 17 00:00:00 2001 From: Jean Helou Date: Fri, 22 Feb 2019 15:59:02 +0100 Subject: Adds test for PlayApi single module --- .../app/controllers/HomeController.scala | 24 ++++++++++++ .../resources/playsingleapi/conf/application.conf | 2 + .../test/resources/playsingleapi/conf/routes | 10 +++++ .../test/controllers/HomeControllerSpec.scala | 45 ++++++++++++++++++++++ 4 files changed, 81 insertions(+) create mode 100644 contrib/playlib/test/resources/playsingleapi/app/controllers/HomeController.scala create mode 100644 contrib/playlib/test/resources/playsingleapi/conf/application.conf create mode 100644 contrib/playlib/test/resources/playsingleapi/conf/routes create mode 100644 contrib/playlib/test/resources/playsingleapi/test/controllers/HomeControllerSpec.scala (limited to 'contrib/playlib/test/resources') diff --git a/contrib/playlib/test/resources/playsingleapi/app/controllers/HomeController.scala b/contrib/playlib/test/resources/playsingleapi/app/controllers/HomeController.scala new file mode 100644 index 00000000..d9fc4bf2 --- /dev/null +++ b/contrib/playlib/test/resources/playsingleapi/app/controllers/HomeController.scala @@ -0,0 +1,24 @@ +package controllers + +import javax.inject._ +import play.api._ +import play.api.mvc._ + +/** + * This controller creates an `Action` to handle HTTP requests to the + * application's home page. + */ +@Singleton +class HomeController @Inject()(cc: ControllerComponents) extends AbstractController(cc) { + + /** + * Create an Action to render an HTML page. + * + * The configuration in the `routes` file means that this method + * will be called when the application receives a `GET` request with + * a path of `/`. + */ + def index() = Action { implicit request: Request[AnyContent] => + Ok("Welcome to Play") + } +} diff --git a/contrib/playlib/test/resources/playsingleapi/conf/application.conf b/contrib/playlib/test/resources/playsingleapi/conf/application.conf new file mode 100644 index 00000000..233bcc90 --- /dev/null +++ b/contrib/playlib/test/resources/playsingleapi/conf/application.conf @@ -0,0 +1,2 @@ +# https://www.playframework.com/documentation/latest/Configuration +play.http.secret.key="foobarbaz" diff --git a/contrib/playlib/test/resources/playsingleapi/conf/routes b/contrib/playlib/test/resources/playsingleapi/conf/routes new file mode 100644 index 00000000..2ac6b336 --- /dev/null +++ b/contrib/playlib/test/resources/playsingleapi/conf/routes @@ -0,0 +1,10 @@ +# Routes +# This file defines all application routes (Higher priority routes first) +# https://www.playframework.com/documentation/latest/ScalaRouting +# ~~~~ + +# An example controller showing a sample home page +GET / controllers.HomeController.index + +# Map static resources from the /public folder to the /assets URL path +GET /assets/*file controllers.Assets.versioned(path="/public", file: Asset) diff --git a/contrib/playlib/test/resources/playsingleapi/test/controllers/HomeControllerSpec.scala b/contrib/playlib/test/resources/playsingleapi/test/controllers/HomeControllerSpec.scala new file mode 100644 index 00000000..97947556 --- /dev/null +++ b/contrib/playlib/test/resources/playsingleapi/test/controllers/HomeControllerSpec.scala @@ -0,0 +1,45 @@ +package controllers + +import org.scalatestplus.play._ +import org.scalatestplus.play.guice._ +import play.api.test._ +import play.api.test.Helpers._ + +/** + * Add your spec here. + * You can mock out a whole application including requests, plugins etc. + * + * For more information, see https://www.playframework.com/documentation/latest/ScalaTestingWithScalaTest + */ +class HomeControllerSpec extends PlaySpec with GuiceOneAppPerTest with Injecting { + + "HomeController GET" should { + + "render the index page from a new instance of controller" in { + val controller = new HomeController(stubControllerComponents()) + val home = controller.index().apply(FakeRequest(GET, "/")) + + status(home) mustBe OK + contentType(home) mustBe Some("text/html") + contentAsString(home) must include ("Welcome to Play") + } + + "render the index page from the application" in { + val controller = inject[HomeController] + val home = controller.index().apply(FakeRequest(GET, "/")) + + status(home) mustBe OK + contentType(home) mustBe Some("text/html") + contentAsString(home) must include ("Welcome to Play") + } + + "render the index page from the router" in { + val request = FakeRequest(GET, "/") + val home = route(app, request).get + + status(home) mustBe OK + contentType(home) mustBe Some("text/html") + contentAsString(home) must include ("Welcome to Play") + } + } +} -- cgit v1.2.3