我想显示scala测试中抛出的Exception消息.
" iWillThrowCustomException Method Failure test.
" should "Fail, Reason: Reason for failing. " in {
evaluating {
iWillThrowCustomException();
} should produce [CustomException]
}
Run Code Online (Sandbox Code Playgroud)
如果CustomExeption将为不同的输入抛出不同类型的消息,比如说
(for -ve amount - 金额小于零,金额为chars - 金额无效),
如何显示块中抛出的消息,因为它将通过CustomException它将显示测试成功,但是它为哪个senario抛出了错误
Tom*_*icz 11
或者你可以看看intercept:
val ex = intercept[CustomException] {
iWillThrowCustomException()
}
ex.getMessage should equal ("My custom message")
Run Code Online (Sandbox Code Playgroud)
evaluating还会返回一个异常,以便您可以检查它或打印消息.以下是ScalaDoc的示例:
val thrown = evaluating { s.charAt(-1) } should produce [IndexOutOfBoundsException]
thrown.getMessage should equal ("String index out of range: -1")
Run Code Online (Sandbox Code Playgroud)
据我所知,您不能在测试名称中包含异常消息.
您可以做的是添加有关测试的其他信息info():
"iWillThrowCustomException Method Failure test." in {
val exception = evaluating { iWillThrowCustomException() } should produce [CustomException]
info("Reason: " + exception.getMessage)
}
Run Code Online (Sandbox Code Playgroud)
这将在测试结果中显示为嵌套消息.您可以在ScalaDoc中找到更多相关信息.