第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

斷言拋出多個(gè)異常

斷言拋出多個(gè)異常

慕桂英546537 2022-08-17 15:53:02
誰(shuí)能告訴我如何使用斷言拋出有幾個(gè)例外?對(duì)于ex,這里有一個(gè)類: 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));    }}和我的測(cè)試方法: @Testvoid checkViolation(){    comptabiliteManager = spy(ComptabiliteManagerImpl.class);    when(vViolations.isEmpty()).thenReturn(false);    assertThrows(  ConstraintViolationException.class, () ->comptabiliteManager.checkViolation(vViolations), "a string should be provided!");}我想匹配方法并完全拋出 ConstraintViolationException 和 FunctionalException有什么想法嗎?
查看完整描述

2 回答

?
白豬掌柜的

TA貢獻(xiàn)1893條經(jīng)驗(yàn) 獲得超10個(gè)贊

將引發(fā)一個(gè)異常,其類型為 。這是一個(gè).FunctionalExceptioncauseFunctionalExceptionConstraintViolationException

假設(shè)是 JUnit 5 方法,它將返回引發(fā)的異常。因此,您可以簡(jiǎn)單地獲取其原因并對(duì)此原因添加其他檢查。assertThrows


查看完整回答
反對(duì) 回復(fù) 2022-08-17
?
慕村225694

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多流暢的方法。


查看完整回答
反對(duì) 回復(fù) 2022-08-17
  • 2 回答
  • 0 關(guān)注
  • 149 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)