Skip to content

Instantly share code, notes, and snippets.

Created September 25, 2015 16:30
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/04158ab2b087dab7288c to your computer and use it in GitHub Desktop.
Save anonymous/04158ab2b087dab7288c to your computer and use it in GitHub Desktop.
name:="futures-test"
scalaVersion:="2.11.7"
libraryDependencies += "org.scalatest" %% "scalatest" % "2.2.5"
package future_test
import scala.concurrent.{ExecutionContext, Future}
object ExampleCode {
def execute()(implicit ec: ExecutionContext): Future[String] = {
Future { throw new TestException("Some data") }
}
}
class TestException(data: String) extends RuntimeException("Expected in test")
package future_test
import org.scalatest.concurrent.ScalaFutures
import org.scalatest.{ShouldMatchers, WordSpec}
class ExampleTest extends WordSpec with ShouldMatchers with ScalaFutures{
import scala.concurrent.ExecutionContext.Implicits.global
".futureValue when Future contains an exception" should {
"allow to test on that exception class" in {
a[TestException] should be thrownBy(ExampleCode.execute().futureValue)
}
"allow to test on RuntimeException" in {
ExampleCode.execute().futureValue
}
}
}
@michaelahlers
Copy link

Came here from your Google Groups thread. Adding a “me, too” here as a market in case one of us comes across a fix.

In my case, this expression:

Await.result(execute(), Duration.Inf)

Throws the expected exception, whereas:

val thrown = the[ExpectedException] thrownBy execute().futureValue

Produces exactly the error you described.

@maowug
Copy link

maowug commented Nov 11, 2016

futureValue warps exceptions as TestFailedException
see https://gist.github.com/maowug/b0e3f564fc9c6290f3367a6650d194d2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment