To test if my code is throwing expected exception I use the following syntax:
an [IllegalArgumentException] should be thrownBy(Algorithms.create(degree))
is there any way to test if thrown exception contains expected message like:
an [IllegalArgumentException(expectedMessage)] should be thrownBy(Algorithms.create(degree)) -- what doesn't compile
?
When you mark a test class with a tag annotation, ScalaTest will mark each test defined in that class with that tag. Thus, marking the SetSpec in the above example with the @Ignore tag annotation means that both tests in the class will be ignored.
The try/catch construct is different in Scala than in Java, try/catch in Scala is an expression. The exception in Scala and that results in a value can be pattern matched in the catch block instead of providing a separate catch clause for each different exception.
Documentation:
the [ArithmeticException] thrownBy 1 / 0 should have message "/ by zero"
and using that example:
the [IllegalArgumentException] thrownBy(Algorithms.create(degree)) should have message expectedMessage
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