gpt4 book ai didi

java - Spock 抛出异常测试

转载 作者:搜寻专家 更新时间:2023-10-30 19:42:31 33 4
gpt4 key购买 nike

我用 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/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com