junit 验证日志输出_JUnit规则–引发异常时执行附加验证
junit 驗證日志輸出
在本文中,我將快速向您展示如果您需要解決以下挑戰,那么JUnit規則有多方便
一個方法可以捕獲異常,并且必須在拋出或引發包裝異常之前執行一些額外的任務。
調用額外任務和引發的異常應通過單元測試進行驗證。
這意味著你有一些這樣的代碼
public class MyThrowingClass {private final ExceptionProcessor exceptionProcessor;public MyThrowingClass(ExceptionProcessor exceptionProcessor) {this.exceptionProcessor = exceptionProcessor;}public void runTask() throws NullPointerException {try {// something to do herethrow new NullPointerException("It's null Jim");} catch (NullPointerException e) {exceptionProcessor.process(e); // This call needs to be verifiedthrow e;}} }并在這行中調用
exceptionProcessor.process(e);需要進行驗證以及引發異常。
直截了當……但丑陋
我不會詳細介紹此變體
try {cut.runMyMethod(); } catch(Exception e) {verify(...);assertThat(e).isInstanceOf(); }因為我個人盡量避免在測試代碼中嘗試使用catch結構。
容易的第一
驗證引發異常非常容易,JUnit在此處提供了潛在的選項
第一個選項看起來像這樣
@Test(expected = NullPointerException.class) public void myTestWithExpectedParameter() throws Exception {// ... }第二個像這樣
// ... @Rule public ExceptionRule exceptionRule = ExceptionRule.none();// ...@Test public void myTestWithTheExceptionRule() throws Exception {exceptionRule.expect(NullPointerException.class);// ... }不,事情變得越來越復雜
上述測試要求背后的問題如下
執行被測方法之后,您執行的所有verify(…)步驟將不會執行,因為如果引發并捕獲了異常,異常將照常停止其余測試方法的執行。
JUnit救援規則
借助JUnit規則,即使拋出異常,我們也可以輕松地創建一種提供其他驗證步驟的方法。
我知道JUnit已經提供了驗證程序規則,但是我不會使用它。 該類的缺點是在設置時會將驗證邏輯刻錄到其中。
因此,我們需要一個規則,該規則允許我們為每個測試指定在執行測試之后應用的附加驗證邏輯。
一般用法如下所示
@Rule public VerifyRule verifyRule = new VerifyRule();@Mock ExceptionProcessor exceptionProcessor;@Test() public void working() throws Exception {verifyRule.setVerifier(() -> verify(exceptionProcessor).process(any()));// .. }為了使它運行起來,我們需要做一些事情
- 驗證規則
- 可以在驗證規則上設置的任何類型的回調接口
讓我們從回調接口開始
public interface VerifyRuleCallback {void execute() throws Throwable; }如您所見,這里沒有什么特別的。
現在,讓我們專注于VerifyRule
public class VerifyRule implements TestRule {private VerifyRuleCallback verifyRuleCallback;@Overridepublic Statement apply(Statement base, Description description) {return new VerifyRuleStatement(base);}public void setVerifier(VerifyRuleCallback verifyRuleCallback) {this.verifyRuleCallback = verifyRuleCallback;}private class VerifyRuleStatement extends Statement {private final Statement nextStatement;public VerifyRuleStatement(Statement nextStatement) {this.nextStatement = nextStatement;}@Overridepublic void evaluate() throws Throwable {nextStatement.evaluate();verifyRuleCallback.execute();}} }如您所見,它實現了TestRule接口,并提供了一種設置VerifyRuleCallback的方法。 然后在需要執行以運行我們自己的回調評估的VerifyRuleStatement的評估方法中使用該回調。
綁在一起
使用新規則和回調,測試可能看起來像這樣
public class MyThrowingClassShould {@Rulepublic MockitoRule mockitoRule = MockitoJUnit.rule();@InjectMocksMyThrowingClass cut;@MockExceptionProcessor processor;@Rulepublic ExpectedException exception = ExpectedException.none();@Rulepublic VerifyRule verifyRule = new VerifyRule();@Test()public void execute_the_exception_processor_and_rethrow_the_exception_when_it_occur() throws Exception {verifyRule.setVerifier(() -> verify(processor).process(any(NullPointerException.class)));exception.expect(NullPointerException.class);cut.runTask();} }摘要
正如我們已經看到的那樣,JUnit規則提供了一種非常好用的簡便方法,可以在這種情況下(不僅在這種情況下)創建干凈且易于理解的測試代碼。
的JUnit 2016-09-26
彼得·道姆(Peter Daum)翻譯自: https://www.javacodegeeks.com/2016/09/junit-rules-executing-additional-verification-exceptions-thrown.html
junit 驗證日志輸出
總結
以上是生活随笔為你收集整理的junit 验证日志输出_JUnit规则–引发异常时执行附加验证的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 如何识别ddos攻击(怎么分析ddos攻
- 下一篇: 汽车安卓导航工厂设置密码(汽车安卓导航)