Skip to content

Instantly share code, notes, and snippets.

@debasishg
Created June 9, 2021 12:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save debasishg/2191bf510c713bc0f6ac2cf1dad2246e to your computer and use it in GitHub Desktop.
Save debasishg/2191bf510c713bc0f6ac2cf1dad2246e to your computer and use it in GitHub Desktop.
import org.scalatest._
import org.scalatest.concurrent.ScalaFutures
import scala.concurrent.Future
// from https://github.com/d6y/scalatest-future-failure/blob/master/src/test/scala/example.scala
class ExampleSpec extends FlatSpec with Matchers with ScalaFutures {
sealed trait Boom extends Throwable
final case class ExampleFail() extends Boom
def example: Future[Int] = Future.failed(ExampleFail())
// Three tricks here:
// 1. Using whenReady to await for the future
// 2. Using .failed to access the failed exception
// 3. If there's no failure, scalatest will catch NoSuchElement exception from the Future
"A future" should "be able to fail" in {
whenReady(example.failed) { e =>
e shouldBe an [ExampleFail]
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment