为了测试我的代码是否抛出预期的异常,我使用以下语法:
an [IllegalArgumentException] should be thrownBy(Algorithms.create(degree))
有什么方法可以测试抛出的异常是否包含预期的消息,例如:
an [IllegalArgumentException(expectedMessage)] should be thrownBy(Algorithms.create(degree)) -- what doesn't compile
?
最佳答案
the [ArithmeticException] thrownBy 1 / 0 should have message "/ by zero"
并使用该示例:
the [IllegalArgumentException] thrownBy(Algorithms.create(degree)) should have message expectedMessage
关于ScalaTest 抛出通过测试异常消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31215768/