【发布时间】:2020-12-06 06:32:15
【问题描述】:
据我所知,Scala 没有检查异常,即我不需要指定方法将抛出的异常。
我有一个 A 类的方法 a 正在测试中。它调用类b 的方法B。我想测试B 抛出异常时的行为。
class b{
def B()={...}
}
我嘲笑过B
when(mockB.B).thenThrow(new UserDoesNotExistException("exception"))
当我这样做时,我收到错误Checked exception is invalid for this method!
这个答案解释了w.r.t。 Java - throw checked Exceptions from mocks with Mockito
虽然将 UserDoesNotExistException 更改为 RuntimeException 对我有用,但我很想知道是否可以通过抛出 UserDoesNotExistException 进行测试
在我的逻辑中,A 有不同的路径,具体取决于抛出的异常类型,因此我需要从我的测试中抛出特定的异常,而不是抛出通用的 RuntimeException。
【问题讨论】: