2 回答

TA貢獻1893條經驗 獲得超10個贊
將引發一個異常,其類型為 。這是一個.FunctionalException
cause
FunctionalException
ConstraintViolationException
假設是 JUnit 5 方法,它將返回引發的異常。因此,您可以簡單地獲取其原因并對此原因添加其他檢查。assertThrows

TA貢獻1880條經驗 獲得超4個贊
我假設 ConstraintViolationException 將是 FunctionalException 的根本原因。在這種情況下,要檢查是否引發了異常,您可以執行如下操作:
Executable executable = () -> comptabiliteManager.checkViolation(vViolations);
Exception exception = assertThrows(FunctionalException.class, executable);
assertTrue(exception.getCause() instanceof ConstraintViolationException);
另一個可能更干凈的解決方案是使用AssertJ及其API。
Throwable throwable = catchThrowable(() -> comptabiliteManager.checkViolation(vViolations));
assertThat(throwable).isInstanceOf(FunctionalException.class)
.hasCauseInstanceOf(ConstraintViolationException.class);
您必須從 AssertJ 的 Assertions 類導入方法:
import static org.assertj.core.api.Assertions.catchThrowable;
import static org.assertj.core.api.Assertions.assertThat;
我鼓勵您查看此API,因為它具有許多流暢的方法。
添加回答
舉報