Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Play 2.2 - specs2 - How to test futures in play 2.2?

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

like image 285
OliverKK Avatar asked Oct 02 '13 07:10

OliverKK


2 Answers

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
     }
   }
}
like image 107
akkie Avatar answered Oct 31 '22 10:10

akkie


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)
like image 43
OliverKK Avatar answered Oct 31 '22 11:10

OliverKK