trait RecoverMethods extends AnyRef
Offers two methods for transforming futures when exceptions are expected.
This trait offers two methods for testing for expected exceptions in the context of
futures: recoverToSucceededIf and recoverToExceptionIf.
Because this trait is mixed into trait AsyncTestSuite, both of its methods are
available by default in any async-style suite.
If you just want to ensure that a future fails with a particular exception type, and do
not need to inspect the exception further, use recoverToSucceededIf:
recoverToSucceededIf[IllegalStateException] { // Result type: Future[Assertion] emptyStackActor ? Peek }
The recoverToSucceededIf method performs a job similar to
assertThrows, except
in the context of a future. It transforms a Future of any type into a
Future[Assertion] that succeeds only if the original future fails with the specified
exception. Here's an example in the REPL:
scala> import org.scalatest.RecoverMethods._
import org.scalatest.RecoverMethods._
scala> import scala.concurrent.Future
import scala.concurrent.Future
scala> import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.ExecutionContext.Implicits.global
scala> recoverToSucceededIf[IllegalStateException] {
     |   Future { throw new IllegalStateException }
     | }
res0: scala.concurrent.Future[org.scalatest.Assertion] = ...
scala> res0.value
res1: Option[scala.util.Try[org.scalatest.Assertion]] = Some(Success(Succeeded))
Otherwise it fails with an error message similar to those given by assertThrows:
scala> recoverToSucceededIf[IllegalStateException] {
     |   Future { throw new RuntimeException }
     | }
res2: scala.concurrent.Future[org.scalatest.Assertion] = ...
scala> res2.value
res3: Option[scala.util.Try[org.scalatest.Assertion]] =
    Some(Failure(org.scalatest.exceptions.TestFailedException: Expected exception
      java.lang.IllegalStateException to be thrown, but java.lang.RuntimeException
      was thrown))
scala> recoverToSucceededIf[IllegalStateException] {
     |   Future { 42 }
     | }
res4: scala.concurrent.Future[org.scalatest.Assertion] = ...
scala> res4.value
res5: Option[scala.util.Try[org.scalatest.Assertion]] =
    Some(Failure(org.scalatest.exceptions.TestFailedException: Expected exception
      java.lang.IllegalStateException to be thrown, but no exception was thrown))
The recoverToExceptionIf method differs from the recoverToSucceededIf in
its behavior when the assertion succeeds: recoverToSucceededIf yields a Future[Assertion],
whereas recoverToExceptionIf yields a Future[T], where T is the
expected exception type.
recoverToExceptionIf[IllegalStateException] { // Result type: Future[IllegalStateException] emptyStackActor ? Peek }
In other words, recoverToExpectionIf is to
intercept as
recovertToSucceededIf is to assertThrows. The first one allows you to perform further
assertions on the expected exception. The second one gives you a result type that will satisfy the type checker
at the end of the test body. Here's an example showing recoverToExceptionIf in the REPL:
scala> val futureEx =
     |   recoverToExceptionIf[IllegalStateException] {
     |     Future { throw new IllegalStateException("hello") }
     |   }
futureEx: scala.concurrent.Future[IllegalStateException] = ...
scala> futureEx.value
res6: Option[scala.util.Try[IllegalStateException]] =
    Some(Success(java.lang.IllegalStateException: hello))
scala> futureEx map { ex => assert(ex.getMessage == "world") }
res7: scala.concurrent.Future[org.scalatest.Assertion] = ...
scala> res7.value
res8: Option[scala.util.Try[org.scalatest.Assertion]] =
    Some(Failure(org.scalatest.exceptions.TestFailedException: "[hello]" did not equal "[world]"))
- Source
 - RecoverMethods.scala
 
- Alphabetic
 - By Inheritance
 
- RecoverMethods
 - AnyRef
 - Any
 
- Hide All
 - Show All
 
- Public
 - Protected
 
Value Members
-   final  def !=(arg0: Any): Boolean
- Definition Classes
 - AnyRef → Any
 
 -   final  def ##: Int
- Definition Classes
 - AnyRef → Any
 
 -   final  def ==(arg0: Any): Boolean
- Definition Classes
 - AnyRef → Any
 
 -   final  def asInstanceOf[T0]: T0
- Definition Classes
 - Any
 
 -    def clone(): AnyRef
- Attributes
 - protected[lang]
 - Definition Classes
 - AnyRef
 - Annotations
 - @throws(classOf[java.lang.CloneNotSupportedException]) @native()
 
 -   final  def eq(arg0: AnyRef): Boolean
- Definition Classes
 - AnyRef
 
 -    def equals(arg0: AnyRef): Boolean
- Definition Classes
 - AnyRef → Any
 
 -    def finalize(): Unit
- Attributes
 - protected[lang]
 - Definition Classes
 - AnyRef
 - Annotations
 - @throws(classOf[java.lang.Throwable])
 
 -   final  def getClass(): Class[_ <: AnyRef]
- Definition Classes
 - AnyRef → Any
 - Annotations
 - @native()
 
 -    def hashCode(): Int
- Definition Classes
 - AnyRef → Any
 - Annotations
 - @native()
 
 -   final  def isInstanceOf[T0]: Boolean
- Definition Classes
 - Any
 
 -   final  def ne(arg0: AnyRef): Boolean
- Definition Classes
 - AnyRef
 
 -   final  def notify(): Unit
- Definition Classes
 - AnyRef
 - Annotations
 - @native()
 
 -   final  def notifyAll(): Unit
- Definition Classes
 - AnyRef
 - Annotations
 - @native()
 
 -    def recoverToExceptionIf[T <: AnyRef](future: Future[Any])(implicit classTag: ClassTag[T], exCtx: ExecutionContext, pos: Position): Future[T]
Transforms a future of any type into a
Future[T], whereTis a given expected exception type, which succeeds if the given future completes with aFailurecontaining the specified exception type.Transforms a future of any type into a
Future[T], whereTis a given expected exception type, which succeeds if the given future completes with aFailurecontaining the specified exception type.See the main documentation for this trait for more detail and examples.
- future
 A future of any type, which you expect to fail with an exception of the specified type T
- returns
 a Future[T] containing on success the expected exception, or containing on failure a
TestFailedException
 -    def recoverToSucceededIf[T <: AnyRef](future: Future[Any])(implicit classTag: ClassTag[T], exCtx: ExecutionContext, pos: Position): Future[compatible.Assertion]
Transforms a future of any type into a
Future[Assertion]that succeeds if the future completes with aFailurecontaining the specified exception type.Transforms a future of any type into a
Future[Assertion]that succeeds if the future completes with aFailurecontaining the specified exception type.See the main documentation for this trait for more detail and examples.
- future
 A future of any type, which you expect to fail with an exception of the specified type T
- returns
 a Future[Assertion] containing on success the
Succeededsingleton, or containing on failure aTestFailedException
 -   final  def synchronized[T0](arg0: => T0): T0
- Definition Classes
 - AnyRef
 
 -    def toString(): String
- Definition Classes
 - AnyRef → Any
 
 -   final  def wait(): Unit
- Definition Classes
 - AnyRef
 - Annotations
 - @throws(classOf[java.lang.InterruptedException])
 
 -   final  def wait(arg0: Long, arg1: Int): Unit
- Definition Classes
 - AnyRef
 - Annotations
 - @throws(classOf[java.lang.InterruptedException])
 
 -   final  def wait(arg0: Long): Unit
- Definition Classes
 - AnyRef
 - Annotations
 - @throws(classOf[java.lang.InterruptedException]) @native()