淄博哪家公司做网站最好,软件盒子wordpress,个人网站建设教学视频,企业服务平台介绍junit:junitJUnit测试中有多种处理异常的方法。 正如我在以前的一篇文章中所写的那样 #xff0c;我最好的方法是使用org.junit.rules.ExpectedException规则。 基本上#xff0c;规则是对org.junit.Before #xff0c; org.junit.After #xff0c; org.junit.BeforeClass… junit:junit JUnit测试中有多种处理异常的方法。 正如我在以前的一篇文章中所写的那样 我最好的方法是使用org.junit.rules.ExpectedException规则。 基本上规则是对org.junit.Before org.junit.After org.junit.BeforeClass或org.junit.AfterClass注释的方法的替代或补充但它们功能更强大并且功能更多。在项目和课程之间轻松共享。 在本文中我将展示org.junit.rules.ExpectedException规则的更高级用法。 验证异常消息 标准JUnit的org.junit.Test批注提供了expected属性该属性允许您指定Throwable以在测试方法抛出指定类的异常时导致测试方法成功。 在许多情况下这似乎足够了但是如果您要验证异常消息则必须找到其他方法。 使用ExpectedException非常简单 public class ExpectedExceptionsTest {Rulepublic ExpectedException thrown ExpectedException.none();Testpublic void verifiesTypeAndMessage() {thrown.expect(RuntimeException.class);thrown.expectMessage(Runtime exception occurred);throw new RuntimeException(Runtime exception occurred);}
} 在上面的代码片段中我们希望消息contains给定的子字符串。 仅与类型检查相比它更安全。 为什么 假设我们有一个ExceptionThrower 如下所示 class ExceptionsThrower {void throwRuntimeException(int i) {if (i 0) {throw new RuntimeException(Illegal argument: i must be 0);}throw new RuntimeException(Runtime exception occurred);}
} 如您所见方法抛出的两个异常都是RuntimeException 因此如果我们不检查消息则不能100确定方法会抛出哪个异常。 以下测试将通过 Test
public void runtimeExceptionOccurs() {thrown.expect(RuntimeException.class);// opposite to expectedexceptionsThrower.throwRuntimeException(0);
}Test
public void illegalArgumentExceptionOccurs() {thrown.expect(RuntimeException.class);// opposite to expectedexceptionsThrower.throwRuntimeException(1);
} 检查异常中的消息将解决问题并确保您正在验证所需的异常。 与Test注释方法的expected属性相比这已经是一个优势。 但是如果您需要以更复杂的方式验证异常消息怎么办 ExpectedException通过提供Hamcrest匹配器为expectMessage方法而不是字符串允许您这样做。 让我们看下面的例子 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 TypeSafeMatcherString {private String pattern;public MatchesPattern(String pattern) {this.pattern pattern;}Overrideprotected boolean matchesSafely(String item) {return item.matches(pattern);}Overridepublic void describeTo(Description description) {description.appendText(matches pattern ).appendValue(pattern);}Overrideprotected void describeMismatchSafely(String item, Description mismatchDescription) {mismatchDescription.appendText(does not match);}
}验证异常对象 在某些情况下匹配消息可能还不够。 自定义方法可能会有例外您也想对其进行验证。 没问题 ExpectedException允许以expect方法它匹配。 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 TypeSafeMatcherCustomException {private int code;public ExceptionCodeMatches(int code) {this.code code;}Overrideprotected boolean matchesSafely(CustomException item) {return item.getCode() code;}Overridepublic void describeTo(Description description) {description.appendText(expects code ).appendValue(code);}Overrideprotected void describeMismatchSafely(CustomException item, Description mismatchDescription) {mismatchDescription.appendText(was ).appendValue(item.getCode());}
} 请注意我同时实现了TypeSafeMatcher describeTo和describeMismatchSafely方法。 当测试失败时我需要它们产生看起来很漂亮的错误消息。 看下面的例子 java.lang.AssertionError:
Expected: (an instance of java.lang.RuntimeException and expects code 1)but: expects code 1 was 2检查原因 您可以使用ExpectedException做的另一件事是验证引发异常的原因。 这也可以使用自定义匹配器完成 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 TypeSafeMatcherThrowable {private final Class? extends Throwable type;private final String expectedMessage;public CauseMatcher(Class? extends Throwable type, String expectedMessage) {this.type type;this.expectedMessage expectedMessage;}Overrideprotected boolean matchesSafely(Throwable item) {return item.getClass().isAssignableFrom(type) item.getMessage().contains(expectedMessage);}Overridepublic void describeTo(Description description) {description.appendText(expects type ).appendValue(type).appendText( and a message ).appendValue(expectedMessage);}
}摘要 ExpectedException规则是一个强大的功能。 通过添加Hamcrest匹配器您可以轻松地为异常测试创建健壮且可重用的代码。 可以在GitHub上找到代码示例。 还请检查我以前的文章 JUnit中处理异常的3种方法。 选择哪一个 参考 JUnit ExpectedException规则 Codeleak.pl博客上的JCG合作伙伴 Rafal Borowiec的基本知识 除外 。 翻译自: https://www.javacodegeeks.com/2014/03/junit-expectedexception-rule-beyond-basics.htmljunit:junit