9

my way of testing futures was using value1. I migrated to play2.2. I found out, my accustomed way to test is gone. @scala.deprecated("Use scala.concurrent.Promise instead.", "2.2")

Any help would be greatly appreciated.

Oliver

Jason Webb
  • 7,938
  • 9
  • 40
  • 49
OliverKK
  • 505
  • 4
  • 15

3 Answers3

14

You can implement the PlaySpecification trait as described in the documentation. This trait provides a method await. You can also override the default timeout.

import akka.util.Timeout
import scala.concurrent.duration._

class FooSpec extends PlaySpecification {
   override implicit def defaultAwaitTimeout: Timeout = 20.seconds

   "foo" should {
     "handle futures" {
        val result = await(Future(true))

        result should beTrue
     }
   }
}
OliverKK
  • 505
  • 4
  • 15
akkie
  • 2,523
  • 1
  • 20
  • 34
2

You can also override the default timeout for a single test scenario, like so:

import akka.util.Timeout
import scala.concurrent.duration._

class FooSpec {
   "foo" should {
     "handle futures" with DefaultAwaitTimeout {
        override implicit def defaultAwaitTimeout: Timeout = 20.seconds
        val result = await(Future(true))

        result should beTrue
     }
   }
}

To stop your code at a specific position, use

 Thread.sleep(milliseconds)
OliverKK
  • 505
  • 4
  • 15
0

From play 2.4~, ,play.api.test.Helpers._ provides few utilities such as contentAsJson, contentAsString and contentAsBytes which can be used if you are waiting for a Future of type play.api.mvc.Result. This takes care of the waiting but you still need to set the implicit timeout value.

Udara Bentota
  • 91
  • 1
  • 8