Search code examples
playframeworkscalatest

Integration tests on a server on Play Framework 2.5


I am building a REST service on top of Play Framework 2.5

I want to create an integration test to test my service as if accessed by an external client.

Using the documentation here -- I mixed OneServerPerSuite in my testing class and overrode 'app' so:

implicit override lazy val app = new GuiceApplicationBuilder().build()

Thinking that I do NOT wish to override my Router configuration.

import org.scalatest.FreeSpec 

import org.scalatestplus.play.OneServerPerSuite 
import play.api.inject.guice.GuiceApplicationBuilder 
import play.api.libs.ws.WSClient

import scala.concurrent.Await 
import scala.concurrent.duration.Duration


class ServerIntegration extends FreeSpec with OneServerPerSuite {


  implicit override lazy val app = new GuiceApplicationBuilder().build()


  "Server" - {
    "When requested for a reply" - {
      "should give it" in {

        val ws = app.injector.instanceOf[WSClient]

        val resonseFuture = ws.url(s"http://localhost:$port/my/defined/route").get()

        val result = Await.result(resonseFuture, Duration.Inf)

        println(result)
      }}} }

It seems that I managed to launch an HTTP Server, but one that does not have my routes defined. I am getting: 'Action Not Found' reply (Status 404).

When running the server normally ('sbt run'), I am able to access all my defined actions (with a browser).


Solution

  • Found my bug. It's silly but might be helpful to others:

    In my conf/routes file, I defined the routes with an @, like so:

    GET     /                           @com.company.service.controllers.serviceController.index
    

    This caused the route to be static and thats why it wasn't injected (when building the GuiceApplicationBuilder).