3

I want to test that my code which returns twitter.Future throws an IllegalArgumentException when a requirement isn't fulfilled.

I'm using scala test with an implicit conversion from twitter.Future to FutureConcept (similar to how ScalaFutures trait works but for twitter Futures)

But I can't find how it should be done!

I've tried the following:

whenReady(methodThrowingExceptionFromFuture) {...handling}

This throws the exception as a TestFailedException before reaching handling part so it doesn't intercept the exception as needed. So I tried intercept:

intercept[IllegalArgumentException] { future.futureValue }

But same here. I think FutureConcept wraps the exception as a TestFailedException so I was thinking of unpacking the real exception, but surely there must be some other way of dealing with negative test cases together with scala futures?

ChillyPro
  • 172
  • 7
  • The implicit conversion of futures to twitter.futures is taken from here: http://cjwebb.github.io/blog/2015/02/02/scalatest-futures/ – ChillyPro Feb 16 '15 at 15:28

1 Answers1

2

I found the answer hidden in this response. Basically, pass a failed projection on your original future to whenReady and kin.

whenReady(methodThrowingExceptionFromFuture.failed) { ...handling... }

Or to just test that an expected exception type was thrown:

methodThrowingExceptionFromFuture.failed.futureValue shouldBe a [MyException]
Community
  • 1
  • 1
Sean
  • 29,130
  • 4
  • 80
  • 105
  • 1
    If I understand your answer correct it refers to Scala futures. Since we use Twitter futures there isn't any failed projection available. – ChillyPro Aug 19 '15 at 15:08
  • You just need to implement the failed method. This is how it is implemented in Scala Futures: https://github.com/scala/scala/blob/v2.11.7/src/library/scala/concurrent/Future.scala#L187 – Simão Martins Feb 17 '16 at 16:04