2 回答

TA貢獻(xiàn)1893條經(jīng)驗(yàn) 獲得超10個(gè)贊
將引發(fā)一個(gè)異常,其類型為 。這是一個(gè).FunctionalException
cause
FunctionalException
ConstraintViolationException
假設(shè)是 JUnit 5 方法,它將返回引發(fā)的異常。因此,您可以簡(jiǎn)單地獲取其原因并對(duì)此原因添加其他檢查。assertThrows

TA貢獻(xiàn)1880條經(jīng)驗(yàn) 獲得超4個(gè)贊
我假設(shè) ConstraintViolationException 將是 FunctionalException 的根本原因。在這種情況下,要檢查是否引發(fā)了異常,您可以執(zhí)行如下操作:
Executable executable = () -> comptabiliteManager.checkViolation(vViolations);
Exception exception = assertThrows(FunctionalException.class, executable);
assertTrue(exception.getCause() instanceof ConstraintViolationException);
另一個(gè)可能更干凈的解決方案是使用AssertJ及其API。
Throwable throwable = catchThrowable(() -> comptabiliteManager.checkViolation(vViolations));
assertThat(throwable).isInstanceOf(FunctionalException.class)
.hasCauseInstanceOf(ConstraintViolationException.class);
您必須從 AssertJ 的 Assertions 類導(dǎo)入方法:
import static org.assertj.core.api.Assertions.catchThrowable;
import static org.assertj.core.api.Assertions.assertThat;
我鼓勵(lì)您查看此API,因?yàn)樗哂性S多流暢的方法。
添加回答
舉報(bào)