scalaunit-testingmockitospecs2

specs2 - how to swallow exceptions


I am using specs2 as my test framework. I want to simulate a use case in which actionA return a failing future. Like this:

  val actionA = mock[ActionA]
  val actionB = new ActionB(actionA)
  actionA.doSomthing(x) returns Future.failed(new Exception("bla"))
  try {
    Await.result(actionB.doSomthing(request), 1 seconds)
  }catch {
    case e: Exception => println("exception caught: " + e);
  }

The problem is that my test exit with this exception if I am not catching it, doesn't specs2 have a nicer way to swallow exceptions? Is there a better way to test such scenario?

Thanks!


Solution

  • Await.result(actionB.doSomething(request), 1 seconds) must throwA[Exception]
    

    via https://etorreborre.github.io/specs2/guide/SPECS2-5.2.0/org.specs2.guide.Matchers.html#for-exception-values -> Exception