网站转换率,哈尔滨建筑工程招聘信息,专业的佛山网站建设公司,梧州网站建设2k9网络团队提供高品质网站建设服务java junit 异常在JUnit中#xff0c;有许多方法可以在测试代码中测试异常#xff0c;包括try-catch idiom JUnit Rule和catch-exception库。 从Java 8开始#xff0c;我们还有另一种处理异常的方法#xff1a;使用lambda表达式。 在这篇简短的博客文章中#xff0c;我将演… java junit 异常 在JUnit中有许多方法可以在测试代码中测试异常包括try-catch idiom JUnit Rule和catch-exception库。 从Java 8开始我们还有另一种处理异常的方法使用lambda表达式。 在这篇简短的博客文章中我将演示一个简单的示例说明如何利用Java 8和lambda表达式的功能来测试JUnit中的异常。 注意撰写此博客文章的动机是在catch-exception项目页面上发布的消息 Java 8的lambda表达式将使catch-exception冗余。 因此该项目将不再维护 SUT –被测系统 我们将测试以下2类抛出的异常。 第一个 class DummyService {public void someMethod() {throw new RuntimeException(Runtime exception occurred);}public void someOtherMethod() {throw new RuntimeException(Runtime exception occurred,new IllegalStateException(Illegal state));}
} 第二个 class DummyService2 {public DummyService2() throws Exception {throw new Exception(Constructor exception occurred);}public DummyService2(boolean dummyParam) throws Exception {throw new Exception(Constructor exception occurred);}
}所需语法 我的目标是实现与catch-exception库接近的语法 package com.github.kolorobot.exceptions.java8;import org.junit.Test;
import static com.github.kolorobot.exceptions.java8.ThrowableAssertion.assertThrown;public class Java8ExceptionsTest {Testpublic void verifiesTypeAndMessage() {assertThrown(new DummyService()::someMethod) // method reference// assertions.isInstanceOf(RuntimeException.class).hasMessage(Runtime exception occurred).hasNoCause();}Testpublic void verifiesCauseType() {assertThrown(() - new DummyService().someOtherMethod(true)) // lambda expression// assertions.isInstanceOf(RuntimeException.class).hasMessage(Runtime exception occurred).hasCauseInstanceOf(IllegalStateException.class);}Testpublic void verifiesCheckedExceptionThrownByDefaultConstructor() {assertThrown(DummyService2::new) // constructor reference// assertions.isInstanceOf(Exception.class).hasMessage(Constructor exception occurred);}Testpublic void verifiesCheckedExceptionThrownConstructor() {assertThrown(() - new DummyService2(true)) // lambda expression// assertions.isInstanceOf(Exception.class).hasMessage(Constructor exception occurred);}Test(expected ExceptionNotThrownAssertionError.class) // making test passpublic void failsWhenNoExceptionIsThrown() {// expected exception not thrownassertThrown(() - System.out.println());}
} 注意与catch-exception相比的优势在于我们将能够测试引发异常的构造函数。 创建“图书馆” 合成糖 assertThrown是一个静态工厂方法它创建一个ThrowableAssertion的新实例并引用了捕获的异常。 package com.github.kolorobot.exceptions.java8;public class ThrowableAssertion {public static ThrowableAssertion assertThrown(ExceptionThrower exceptionThrower) {try {exceptionThrower.throwException();} catch (Throwable caught) {return new ThrowableAssertion(caught);}throw new ExceptionNotThrownAssertionError();}// other methods omitted for now
} ExceptionThrower是FunctionalInterface 可以使用lambda表达式方法引用或构造函数引用创建实例。 assertThrown接受ExceptionThrower将期望并准备处理异常。 FunctionalInterface
public interface ExceptionThrower {void throwException() throws Throwable;
}断言 最后我们需要创建一些断言以便可以在测试代码中验证有关teste异常的解释。 实际上 ThrowableAssertion是一种自定义断言为我们提供了一种有效地验证所捕获异常的方法。 在下面的代码中我使用了Hamcrest匹配器来创建断言。 ThrowableAssertion类的完整来源 package com.github.kolorobot.exceptions.java8;import org.hamcrest.Matchers;
import org.junit.Assert;public class ThrowableAssertion {public static ThrowableAssertion assertThrown(ExceptionThrower exceptionThrower) {try {exceptionThrower.throwException();} catch (Throwable caught) {return new ThrowableAssertion(caught);}throw new ExceptionNotThrownAssertionError();}private final Throwable caught;public ThrowableAssertion(Throwable caught) {this.caught caught;}public ThrowableAssertion isInstanceOf(Class? extends Throwable exceptionClass) {Assert.assertThat(caught, Matchers.isA((ClassThrowable) exceptionClass));return this;}public ThrowableAssertion hasMessage(String expectedMessage) {Assert.assertThat(caught.getMessage(), Matchers.equalTo(expectedMessage));return this;}public ThrowableAssertion hasNoCause() {Assert.assertThat(caught.getCause(), Matchers.nullValue());return this;}public ThrowableAssertion hasCauseInstanceOf(Class? extends Throwable exceptionClass) {Assert.assertThat(caught.getCause(), Matchers.notNullValue());Assert.assertThat(caught.getCause(), Matchers.isA((ClassThrowable) exceptionClass));return this;}
}AssertJ实施 如果您使用AssertJ库则可以利用org.assertj.core.api.ThrowableAssert轻松创建ThrowableAssertion AssertJ版本它提供了许多org.assertj.core.api.ThrowableAssert断言。 该类的实现比上面介绍的Hamcrest更简单。 package com.github.kolorobot.exceptions.java8;import org.assertj.core.api.Assertions;
import org.assertj.core.api.ThrowableAssert;public class AssertJThrowableAssert {public static ThrowableAssert assertThrown(ExceptionThrower exceptionThrower) {try {exceptionThrower.throwException();} catch (Throwable throwable) {return Assertions.assertThat(throwable);}throw new ExceptionNotThrownAssertionError();}
} AssertJ的示例测试 public class AssertJJava8ExceptionsTest {Testpublic void verifiesTypeAndMessage() {assertThrown(new DummyService()::someMethod).isInstanceOf(RuntimeException.class).hasMessage(Runtime exception occurred).hasMessageStartingWith(Runtime).hasMessageEndingWith(occurred).hasMessageContaining(exception).hasNoCause();}
}摘要 仅用几行代码我们构建了非常酷的代码可帮助我们在JUnit中测试异常而无需任何其他库。 这仅仅是一个开始。 利用Java 8和lambda表达式的强大功能 资源资源 GitHub上提供了本文的源代码 请看com.github.kolorobot.exceptions.java8包 我的其他一些文章关于在JUnit中测试异常。 请看一看 定制断言 翻译自: https://www.javacodegeeks.com/2014/07/junit-testing-exception-with-java-8-and-lambda-expressions.htmljava junit 异常