我使用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
块中,我捕获了这个异常并抛出了一个CustomException
,因此我期望我的方法将抛出CustomException
而不是AnotherCustomExceptio
。我该如何测试呢?
bean#methodName
的调用)。很可能,异常并未从上面显示的 try 块中抛出。您应该能够在调试器中验证这一点。 - Peter Niederwieser