我试图在使用Matchers的测试中断言异常类型(不要问为什么),我得到的解决方案是:
exception.getClass shouldBe classOf[FileNotFoundException]
但它看起来非常丑陋,有更好的方法吗?
再见
答案 0 :(得分:1)
一种可能的解决方案是使用intercept
方法:
val exception = intercept[NoSuchElementException] {
List.empty[String].head // Code that throws exception
}
exception.getMessage shouldBe "head of empty list"
答案 1 :(得分:1)
你可以“an []应该被抛出”匹配器:
an [IllegalArgumentException] should be thrownBy {
//code that should raise an exception here
}
确保您的测试类包含“Matchers”:
class MyTestClass extends FunSuite with Matchers