user4955663
user4955663

Reputation: 1071

Playframework: Testing with a server example does not compile

There is a "Testing with a server" example how to test Play framework application with a real HTTP stack.

I tried to compile the example with Play 2.5.2 and Scala 2.11.7 without success. Original example was without imports. Here is the example code with imports I added to get is (almost) compile:

package models

import org.scalatestplus.play.{OneServerPerSuite, PlaySpec}
import play.api.cache.EhCacheModule
import play.api.inject.guice.GuiceApplicationBuilder
import play.api.libs.ws.WSClient
import play.api.routing.sird._
import play.api.routing._
import play.api.mvc._
import play.api.mvc.Results._

object TestExp04 {

}

class ExampleSpec extends PlaySpec with OneServerPerSuite {

  // Override app if you need an Application with other than
  // default parameters.
  implicit override lazy val app =
    new GuiceApplicationBuilder().disable[EhCacheModule].router(Router.from {
      case GET(p"/") => Action { Ok("ok") }
    }).build()

  "test server logic" in {
    val wsClient = app.injector.instanceOf[WSClient]
    val myPublicAddress =  s"localhost:$port"
    val testPaymentGatewayURL = s"http://$myPublicAddress"
    // The test payment gateway requires a callback to this server before it returns a result...
    val callbackURL = s"http://$myPublicAddress/callback"
    // await is from play.api.test.FutureAwaits
    val response = await(wsClient.url(testPaymentGatewayURL).withQueryString("callbackURL" -> callbackURL).get())

    response.status mustBe OK
  }
}

And the final compiler error is:

[error] /home/js/workspace/example/server/test/models/TestExp04.scala:32: not found: value await
[error]     val response = await(wsClient.url(testPaymentGatewayURL).withQueryString("callbackURL" -> callbackURL).get())
[error]                    ^

What is the mysterious await? What should I import to get this running?

As a feedback to the example writers I would say that please do not strip the imports away when you prepare the example. Sometimes it may take lot of time to guess the proper combination of imports.

Upvotes: 1

Views: 477

Answers (3)

user3630078
user3630078

Reputation:

Alternatively you could also use,

scala.concurrent.Await.result(scala.concurrent.Future<actually it takes super class Awaitable>, 
    scala.concurrent.Duration)

which gives you a nice way to timeout the test if it takes longer than specified duration.

Upvotes: 1

user4955663
user4955663

Reputation: 1071

Actually the answer was earlier in the documentation in place "Writing functional tests with ScalaTest"

where it proposed to import all helpers:

import org.scalatest._
import org.scalatestplus.play._
import play.api.test._
import play.api.test.Helpers.{GET => GET_REQUEST, _}

The last import here solved the problem. So the necessary imports are:

import play.api.test.Helpers.{GET => GET_REQUEST, _}
import org.scalatestplus.play.{OneServerPerSuite, PlaySpec}
import play.api.cache.EhCacheModule
import play.api.inject.guice.GuiceApplicationBuilder
import play.api.libs.ws.WSClient
import play.api.routing.sird._
import play.api.routing._
import play.api.mvc._
import play.api.mvc.Results._

Upvotes: 1

stan
stan

Reputation: 4995

Import play.api.test.FutureAwaits.

import play.api.test.FutureAwaits._

Upvotes: 0

Related Questions