Play 2.2 - specs2 - How to test futures in play 2.2?
Asked Answered
H

3

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

Homozygous answered 2/10, 2013 at 7:31 Comment(0)
R
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
     }
   }
}
Rausch answered 13/1, 2014 at 14:23 Comment(0)
H
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)
Homozygous answered 29/1, 2015 at 11:54 Comment(0)
M
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.

Murk answered 24/7, 2018 at 22:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.