junit:junit_处理JUnit中异常的另一种方法:catch-exception
junit:junit
JUnit中有許多處理異常的方法 (JUnit中有3種處理異常的方法。選擇哪一種? JUnit ExpectedException規則:超越了基礎 )。 在這篇文章中,我將介紹建議嘗試的catch-exception庫。 簡而言之, catch-exceptions是一個庫,可在一行代碼中捕獲異常,并使它們可用于進一步分析。
通過Maven安裝
為了快速入門,我使用了帶有一組測試依賴項( JUnit,Mocito,Hamcrest,AssertJ )的單元測試演示項目,并添加了catch-exceptions :
<dependency><groupId>com.googlecode.catch-exception</groupId><artifactId>catch-exception</artifactId><version>1.2.0</version><scope>test</scope> </dependency>因此,依賴關系樹如下所示:
[INFO] --- maven-dependency-plugin:2.1:tree @ unit-testing-demo --- [INFO] com.github.kolorobot:unit-testing-demo:jar:1.0.0-SNAPSHOT [INFO] +- org.slf4j:slf4j-api:jar:1.5.10:compile [INFO] +- org.slf4j:jcl-over-slf4j:jar:1.5.10:runtime [INFO] +- org.slf4j:slf4j-log4j12:jar:1.5.10:runtime [INFO] +- log4j:log4j:jar:1.2.15:runtime [INFO] +- junit:junit:jar:4.11:test [INFO] +- org.mockito:mockito-core:jar:1.9.5:test [INFO] +- org.assertj:assertj-core:jar:1.5.0:test [INFO] +- org.hamcrest:hamcrest-core:jar:1.3:test [INFO] +- org.hamcrest:hamcrest-library:jar:1.3:test [INFO] +- org.objenesis:objenesis:jar:1.3:test [INFO] \- com.googlecode.catch-exception:catch-exception:jar:1.2.0:test入門
被測系統(SUT):
class ExceptionThrower {void someMethod() {throw new RuntimeException("Runtime exception occurred");}void someOtherMethod() {throw new RuntimeException("Runtime exception occurred",new IllegalStateException("Illegal state"));}void yetAnotherMethod(int code) {throw new CustomException(code);} }帶有AssertJ斷言的基本catch-exception BDD樣式方法示例:
import org.junit.Test;import static com.googlecode.catchexception.CatchException.*; import static com.googlecode.catchexception.apis.CatchExceptionAssertJ.*;public class CatchExceptionsTest {@Testpublic void verifiesTypeAndMessage() {when(new SomeClass()).someMethod();then(caughtException()).isInstanceOf(RuntimeException.class).hasMessage("Runtime exception occurred").hasMessageStartingWith("Runtime").hasMessageEndingWith("occured").hasMessageContaining("exception").hasNoCause(); } }看起來挺好的。 簡潔,可讀。 沒有JUnit運行者。 請注意,我指定了我希望引發異常的SomeClass方法。 可以想象,我可以在一個測試中檢查多個異常。 盡管我不推薦這種方法,因為這可能違反了測試的單一責任。
順便說一句,如果您正在使用Eclipse,這可能對您來說很方便: 在Eclipse中創建JUnit測試時,改進具有靜態成員類型的內容輔助
查明原因
我認為以下代碼無需評論:
import org.junit.Test;import static com.googlecode.catchexception.CatchException.*; import static com.googlecode.catchexception.apis.CatchExceptionAssertJ.*;public class CatchExceptionsTest {@Testpublic void verifiesCauseType() {when(new ExceptionThrower()).someOtherMethod();then(caughtException()).isInstanceOf(RuntimeException.class).hasMessage("Runtime exception occurred").hasCauseExactlyInstanceOf(IllegalStateException.class).hasRootCauseExactlyInstanceOf(IllegalStateException.class);} }驗證Hamcrest的自定義例外
為了驗證我用我以前的Hamcrest匹配代碼自定義異常后 :
class CustomException extends RuntimeException {private final int code;public CustomException(int code) {this.code = code;}public int getCode() {return code;} }class ExceptionCodeMatches extends TypeSafeMatcher<CustomException> {private int expectedCode;public ExceptionCodeMatches(int expectedCode) {this.expectedCode = expectedCode;}@Overrideprotected boolean matchesSafely(CustomException item) {return item.getCode() == expectedCode;}@Overridepublic void describeTo(Description description) {description.appendText("expects code ").appendValue(expectedCode);}@Overrideprotected void describeMismatchSafely(CustomException item, Description mismatchDescription) {mismatchDescription.appendText("was ").appendValue(item.getCode());} }和測試:
import org.junit.Test;import static com.googlecode.catchexception.CatchException.*; import static org.junit.Assert.*;public class CatchExceptionsTest {@Testpublic void verifiesCustomException() {catchException(new ExceptionThrower(), CustomException.class).yetAnotherMethod(500);assertThat((CustomException) caughtException(), new ExceptionCodeMatcher(500));} }摘要
捕獲異常看起來真的很好。 快速入門很容易。 我看到了一些優于JUnit方法規則的優點。 如果有機會,我將更徹底地調查圖書館,希望在一個實際項目中進行。
- 可以在這里找到本文的源代碼: 單元測試演示
如果您有興趣,請查看我的其他帖子:
- 在JUnit中處理異常的3種方式。 選擇哪一個?
- JUnit ExpectedException規則:超越基礎
- 如何:在Maven項目(JUnit,Mocito,Hamcrest,AssertJ)中測試依賴項
- 在Eclipse中創建JUnit測試時,改進具有靜態成員類型的內容輔助
翻譯自: https://www.javacodegeeks.com/2014/04/yet-another-way-to-handle-exceptions-in-junit-catch-exception.html
junit:junit
創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎總結
以上是生活随笔為你收集整理的junit:junit_处理JUnit中异常的另一种方法:catch-exception的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 美国满意度指数报告出炉:苹果蝉联 20
- 下一篇: 2024款小鹏G9领航上市26.39万元