ScalaTest: Assert exceptions in failed futures (non-blocking)

前端 未结 6 686
夕颜
夕颜 2020-12-24 10:54
import org.scalatest.{ FlatSpec, Matchers, ParallelTestExecution }
import org.scalatest.concurrent.ScalaFutures
import org.apache.thrift.TApplicationException

class         


        
6条回答
  •  一整个雨季
    2020-12-24 11:19

    I know this is probably a bit late, but ScalaTest provides this feature out of the box (I believe since version 2) by mixing in the ScalaFutures trait, or using it directly in your test functions. Behold!

    test("some test") {
      val f: Future[Something] = someObject.giveMeAFuture
      ScalaFutures.whenReady(f.failed) { e =>
        e shouldBe a [SomeExceptionType]
      }
    }
    

    Or you can perform some other assertions in there. Basically, if your future doesn't fail like you expect, the test will fail. If it fails, but throws a different exception, the test will fail. Nice and easy! =]


    cheeky edit:

    You can also use this method to test anything that returns a future:

    test("some test") {
      val f: Future[Something] = someObject.giveMeAFuture
      ScalaFutures.whenReady(f) { s =>
        // run assertions against the object returned in the future
      }
    }
    

    Most recent edit!

    I just wanted to update this answer with more useful information based on newer versions of Scala test. The various spec traits now all have async support, so instead of extending, say, WordSpec, you would instead extend AsyncWordSpec, and instead of relying on the whenReady calls as above, you would just map over your futures directly in the test.

    Example:

    class SomeSpec extends Async[*]Spec with Matchers {
    
    ...
    
      test("some test") {
        someObject.funcThatReturnsAFutureOfSomething map { something =>
          // run assertions against the 'something' returned in the future
        }
      }
    }
    

提交回复
热议问题