加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

如何在ScalaTest中显示“should produce [exception]”中抛出的

发布时间:2020-12-16 19:22:33 所属栏目:安全 来源:网络整理
导读:我想显示 scala测试中抛出的Exception消息. " iWillThrowCustomException Method Failure test. " should "Fail,Reason: Reason for failing. " in { evaluating { iWillThrowCustomException(); } should produce [CustomException]} 如果CustomExeption将
我想显示 scala测试中抛出的Exception消息.

" iWillThrowCustomException Method Failure test.   
 " should "Fail,Reason: Reason for failing. " in {
 evaluating {
      iWillThrowCustomException();
   } should produce [CustomException]
}

如果CustomExeption将为不同的输入抛出不同类型的消息,比如说

(for -ve amount – 金额小于零,金额为chars – 金额无效),

如何显示块中抛出的消息,因为它将通过
???CustomException它将显示Test Success,但是它为哪个senario抛出了错误

解决方法

evaluate还会返回一个异常,以便您可以检查它或打印消息.以下是 ScalaDoc的示例:

val thrown = evaluating { s.charAt(-1) } should produce [IndexOutOfBoundsException]
thrown.getMessage should equal ("String index out of range: -1")

据我所知,您不能在测试名称中包含异常消息.

您可以做的是使用info()添加有关测试的其他信息:

"iWillThrowCustomException Method Failure test." in {
    val exception = evaluating { iWillThrowCustomException() } should produce [CustomException]
    info("Reason: " + exception.getMessage)
}

这将在测试结果中显示为嵌套消息.你可以找到更多关于这个in ScalaDoc的信息.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读