在Junit中处置责罚异常的体式格式有许多种,好比:
有人举荐我考试考试一下catch-exception,在这篇文章中我将会向人人引见。简而言之,catch-exception库可以仅在一行代码中捕捉异常,往后再对个中止处置责罚。
经由进程Maven安装
com.谷歌code.catch-exception
catch-exception
1.2.0
test
依存关系树以下:
[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.谷歌code.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断言的BDD-style方法catch-exception示例:
import org.junit.Test;
import static com.谷歌code.catchexception.CatchException.*;
import static com.谷歌code.catchexception.apis.CatchExceptionAssertJ.*;
public class CatchExceptionsTest {
@Test
public void verifiesTypeAndMessage() {
when(new SomeClass()).someMethod();
then(caughtException())
.isInstanceOf(RuntimeException.class)
.hasMessage("Runtime exception occurred")
.hasMessageStartingWith("Runtime")
.hasMessageEndingWith("occured")
.hasMessageContaining("exception")
.hasNoCause();
}
}
看起来不错——庞杂,可读性高。没有JUnit运转。请留意,我指定的谁人在SomeClass类中抛出异常的方法。可想而知,我可以在一个测试中考验多个异常。然则这背反了测试中的单一责任绳尺,所以不举荐这类做法。其他,假定你是用这篇文章对你有用。
搜检异常的启事
我信任上面的代码就没有需求谈论了吧:
import org.junit.Test;
import static com.谷歌code.catchexception.CatchException.*;
import static com.谷歌code.catchexception.apis.CatchExceptionAssertJ.*;
public class CatchExceptionsTest {
@Test
public 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 {
private int expectedCode;
public ExceptionCodeMatches(int expectedCode) {
this.expectedCode = expectedCode;
}
@Override
protected boolean matchesSafely(CustomException item) {
return item.getCode() == expectedCode;
}
@Override
public void describeTo(Description description) {
description.appendText("expects code ")
.appendValue(expectedCode);
}
@Override
protected void describeMismatchSafely(CustomException item, Description mismatchDescription) {
mismatchDescription.appendText("was ")
.appendValue(item.getCode());
}
}
测试部份:
import org.junit.Test;
import static com.谷歌code.catchexception.CatchException.*;
import static org.junit.Assert.*;
public class CatchExceptionsTest {
@Test
public void verifiesCustomException() {
catchException(new ExceptionThrower(), CustomException.class).yetAnotherMethod(500);
assertThat((CustomException) caughtException(), new ExceptionCodeMatcher(500));
}
}
总结
catch-exception看起来很棒,上手异常庞杂。比起在JUnit中的方法它具有许多优势。假定无机遇我竟会深切的研讨一下这个库,进展在理想中能有一个这样的机遇。
假定你感愉快喜好,可以看一下我其他的文章:
style="display:inline-block;width:336px;height:280px"
data-ad-client="ca-pub-9611302475373562"
data-ad-slot="8266948139">