gpt4 book ai didi

java - assertThrows 多个异常

转载 作者:行者123 更新时间:2023-11-29 08:23:47 25 4
gpt4 key购买 nike

谁能告诉我如何使用 assertThrows 有几个异常(exception)?

例如,这是一个类:

 protected void checkViolation(Set<ConstraintViolation<EcritureComptable>> vViolations) throws FunctionalException {
if (!vViolations.isEmpty()) {
throw new FunctionalException("L'écriture comptable ne respecte pas les règles de gestion.",
new ConstraintViolationException(
"L'écriture comptable ne respecte pas les contraintes de validation",
vViolations));
}
}

和我的测试方法:

 @Test
void checkViolation(){
comptabiliteManager = spy(ComptabiliteManagerImpl.class);
when(vViolations.isEmpty()).thenReturn(false);

assertThrows( ConstraintViolationException.class, () ->comptabiliteManager.checkViolation(vViolations), "a string should be provided!");
}

我想匹配方法并抛出ConstraintViolationExceptionFunctionalException

有什么想法吗?

谢谢

最佳答案

抛出一个异常,类型为 FunctionalException。此 FunctionalException原因ConstraintViolationException

假设 assertThrowsJUnit 5 method ,它返回抛出的异常。因此,您可以简单地了解其原因并对此原因添加额外的检查。

关于java - assertThrows 多个异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55207946/

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