I would like to display the Exception message thrown in scala test.
" iWillThrowCustomException Method Failure test.
" should "Fail, Reason: Reason for failing. " in {
evaluating {
iWillThrowCustomException();
} should produce [CustomException]
}
If CustomExeption will throw different types of messages for differnt inputs , say
(for -ve amount - Amount is less than zero, for chars in amount - Invalid amount),
how to display the message which is thrown in the block, becuase it will through the CustomException and it will show Test Success, but for which senario it has thrown the error
Alternatively you can check out intercept
:
val ex = intercept[CustomException] {
iWillThrowCustomException()
}
ex.getMessage should equal ("My custom message")
evaluating
also returns an exception so you can inspect it or print the message. Here is example from the ScalaDoc:
val thrown = evaluating { s.charAt(-1) } should produce [IndexOutOfBoundsException]
thrown.getMessage should equal ("String index out of range: -1")
As far as I know, you can't include exception message in the test name.
What you can do, is to add additional information about test with info()
:
"iWillThrowCustomException Method Failure test." in {
val exception = evaluating { iWillThrowCustomException() } should produce [CustomException]
info("Reason: " + exception.getMessage)
}
This will be shown in the test results as nested message. You can find more info about this in ScalaDoc.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With