ScalaTest thrownBy测试异常消息

ScalaTest thrownBy测试异常消息,scala,scalatest,Scala,Scalatest,要测试我的代码是否引发预期的异常,我使用以下语法: an [IllegalArgumentException] should be thrownBy(Algorithms.create(degree)) 是否有任何方法可以测试抛出的异常是否包含以下预期消息: an [IllegalArgumentException(expectedMessage)] should be thrownBy(Algorithms.create(degree)) -- what doesn't compil

要测试我的代码是否引发预期的异常,我使用以下语法:

  an [IllegalArgumentException] should be thrownBy(Algorithms.create(degree))
是否有任何方法可以测试抛出的异常是否包含以下预期消息:

  an [IllegalArgumentException(expectedMessage)] should be thrownBy(Algorithms.create(degree)) -- what doesn't compile 

举个例子:

 the [IllegalArgumentException] thrownBy(Algorithms.create(degree)) should have message expectedMessage

检查消息的语法错误。作为bjfletcher建议的替代方案,您还可以根据文档使用以下内容:

  • 捕获异常并将其分配给val
  • val-thrownBy-Algorithms.create(degree)

  • 使用val检查其内容。例如:

  • thrownBy.getMessage应等于(“我如何检查消息的子字符串?将“thrownBy…”的结果分配给val,然后使用常规匹配器?如果要在子字符串上断言,可以:
    ([ArrithmeticException]thrownBy 1/0)。getMessage应包括(“零”)
     the [IllegalArgumentException] thrownBy(Algorithms.create(degree)) should have message expectedMessage