1

Given a scala function that can produces Failures:

def testableFunction(x: Int) : Try[Int] = {
  if(x == 0)
    Failure[Int](new IllegalArgumentException("0 is bad"))
  else if(x == 1)
    Failure[Int](new Exception("1 not so good either"))
  else
    Success(42)
}

How can scalatest matchers be used to test the Exception type?

I'm looking for something of the form:

testableFunction(0).failure.type should be IllegalArgumentException

testableFunction(1).failure.type should be Exception

Thank you in advance for your consideration and response.

1

1 Answer 1

3

Mix in the TryValues trait:

class ExampleTestSpec extends Matchers with TryValues {
  ...
  testableFunction(0).failure.exception shouldBe a [IllegalArgumentException]
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.