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

如何使用ScalaTest测试预期异常的其他属性

发布时间:2020-12-16 09:02:39 所属栏目:安全 来源:网络整理
导读:我正在使用 ScalaTest来测试一些 Scala代码. 我目前正在使用这样的代码测试预期的异常 import org.scalatest._import org.scalatest.matchers.ShouldMatchersclass ImageComparisonTest extends FeatureSpec with ShouldMatchers{ feature("A test can throw
我正在使用 ScalaTest来测试一些 Scala代码.
我目前正在使用这样的代码测试预期的异常

import org.scalatest._
import org.scalatest.matchers.ShouldMatchers

class ImageComparisonTest extends FeatureSpec with ShouldMatchers{

    feature("A test can throw an exception") {

        scenario("when an exception is throw this is expected"){
            evaluating { throw new Exception("message") } should produce [Exception]
        }
    }
}

但是我想在例外情况下添加额外的检查,例如我想检查一下例外消息是否包含某个字符串.

这样做有“干净”的方法吗?或者我必须使用try catch块吗?

解决方法

我找到了解决方案

val exception = intercept[SomeException]{ ... code that throws SomeException ... }
// you can add more assertions based on exception here

(编辑:李大同)

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

    推荐文章
      热点阅读