我用 Spock 测试 Java 代码。我测试这段代码:
try {
Set<String> availableActions = getSthAction()
List<String> goodActions = getGoodAction()
if (!CollectionUtils.containsAny(availableActions ,goodActions )){
throw new CustomException();
}
} catch (AnotherCustomExceptio e) {
throw new CustomException(e.getMessage());
}
我写了测试:
def "some test"() {
given:
bean.methodName(_) >> {throw new AnotherCustomExceptio ("Sth wrong")}
def order = new Order();
when:
validator.validate(order )
then:
final CustomException exception = thrown()
}
它失败了,因为 AnotherCustomExceptio
被抛出。但是在 try{}catch
block 中我捕获了这个异常并抛出一个 CustomException
所以我预计我的方法将抛出 CustomException
而不是 另一个自定义异常
。我如何测试它?
最佳答案
我相信您的 then
block 需要修复。尝试以下语法:
then:
thrown CustomException
关于java - Spock 抛出异常测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22757014/