public class ExpectedExceptionsTest {
@Rule
public ExpectedException thrown = ExpectedException.none();
@Test
public void verifiesTypeAndMessage() {
thrown.expect(RuntimeException.class);
thrown.expectMessage("Runtime exception occurred");
throw new RuntimeException("Runtime exception occurred");
}
}
class ExceptionsThrower {
void throwRuntimeException(int i) {
if (i <= 0) {
throw new RuntimeException("Illegal argument: i must be <= 0");
}
throw new RuntimeException("Runtime exception occurred");
}
}
@Test
public void runtimeExceptionOccurs() {
thrown.expect(RuntimeException.class);
// opposite to expected
exceptionsThrower.throwRuntimeException(0);
}
@Test
public void illegalArgumentExceptionOccurs() {
thrown.expect(RuntimeException.class);
// opposite to expected
exceptionsThrower.throwRuntimeException(1);
}
@Test
public void verifiesMessageStartsWith() {
thrown.expect(RuntimeException.class);
thrown.expectMessage(startsWith("Illegal argument:"));
throw new RuntimeException("Illegal argument: i must be <= 0");
}
@Test
public void verifiesMessageMatchesPattern() {
thrown.expect(RuntimeException.class);
thrown.expectMessage(new MatchesPattern("[Ii]llegal .*"));
throw new RuntimeException("Illegal argument: i must be <= 0");
}
class MatchesPattern extends TypeSafeMatcher{
private String pattern;
public MatchesPattern(String pattern) {
this.pattern = pattern;
}
@Override
protected boolean matchesSafely(String item) {
return item.matches(pattern);
}
@Override
public void describeTo(Description description) {
description.appendText("matches pattern ")
.appendValue(pattern);
}
@Override
protected void describeMismatchSafely(String item, Description mismatchDescription) {
mismatchDescription.appendText("does not match");
}
}
@Test
public void verifiesCustomException() {
thrown.expect(RuntimeException.class);
thrown.expect(new ExceptionCodeMatches(1));
throw new CustomException(1);
}
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 code;
public ExceptionCodeMatches(int code) {
this.code = code;
}
@Override
protected boolean matchesSafely(CustomException item) {
return item.getCode() == code;
}
@Override
public void describeTo(Description description) {
description.appendText("expects code ")
.appendValue(code);
}
@Override
protected void describeMismatchSafely(CustomException item, Description mismatchDescription) {
mismatchDescription.appendText("was ")
.appendValue(item.getCode());
}
}
java.lang.AssertionError:
Expected: (an instance of java.lang.RuntimeException and expects code <1>)
but: expects code <1> was <2>
2>1>1>
@Test
public void verifiesCauseTypeAndAMessage() {
thrown.expect(RuntimeException.class);
thrown.expectCause(new CauseMatcher(IllegalStateException.class, "Illegal state"));
throw new RuntimeException("Runtime exception occurred",
new IllegalStateException("Illegal state"));
}
private static class CauseMatcher extends TypeSafeMatcher{
private final Class<? extends Throwable> type;
private final String expectedMessage;
public CauseMatcher(Class<? extends Throwable> type, String expectedMessage) {
this.type = type;
this.expectedMessage = expectedMessage;
}
@Override
protected boolean matchesSafely(Throwable item) {
return item.getClass().isAssignableFrom(type)
&& item.getMessage().contains(expectedMessage);
}
@Override
public void describeTo(Description description) {
description.appendText("expects type ")
.appendValue(type)
.appendText(" and a message ")
.appendValue(expectedMessage);
}
}<2>
2>
JUnit:使用ExpectedException进行异常测试,布布扣,bubuko.com
JUnit:使用ExpectedException进行异常测试
原文:http://blog.csdn.net/spidercoco/article/details/22570859