Barak BN
Barak BN

Reputation: 558

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).

Upvotes: 0

Views: 757

Answers (2)

Barak BN
Barak BN

Reputation: 558

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).

Upvotes: 0

Andriy Kuba
Andriy Kuba

Reputation: 8263

Your code is completely correct.

It works as is if put into a project.

You can check it with creation a new project from play-scala template and change the URL in test to count like

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

the result is

[info] Server
[info]   When requested for a reply
AhcWSResponse(200, OK)
[info]   - should give it
[info] application - ApplicationTimer demo: Stopping application at 2016-12-04T19:44:03.761Z after 0s.

So - check the URL again

Upvotes: 0

Related Questions