在JUnit 5中是否有更好的方法断言方法抛出异常?
目前,我必须使用@Rule来验证我的测试是否抛出异常,但这不适用于我希望多个方法在测试中抛出异常的情况。
在JUnit 5中是否有更好的方法断言方法抛出异常?
目前,我必须使用@Rule来验证我的测试是否抛出异常,但这不适用于我希望多个方法在测试中抛出异常的情况。
当前回答
他们在JUnit 5中改变了它(预期:InvalidArgumentException,实际:invoked method),代码看起来像这样:
@Test
public void wrongInput() {
Throwable exception = assertThrows(InvalidArgumentException.class,
()->{objectName.yourMethod("WRONG");} );
}
其他回答
这里有一个简单的方法。
@Test
void exceptionTest() {
try{
model.someMethod("invalidInput");
fail("Exception Expected!");
}
catch(SpecificException e){
assertTrue(true);
}
catch(Exception e){
fail("wrong exception thrown");
}
}
它只有在抛出您期望的异常时才会成功。
您可以使用assertThrows(),它允许您在同一个测试中测试多个异常。由于Java 8支持lambdas,这是JUnit中测试异常的规范方法。
根据JUnit文档:
import static org.junit.jupiter.api.Assertions.assertThrows;
@Test
void exceptionTesting() {
MyException thrown = assertThrows(
MyException.class,
() -> myObject.doThing(),
"Expected doThing() to throw, but it didn't"
);
assertTrue(thrown.getMessage().contentEquals("Stuff"));
}
他们在JUnit 5中改变了它(预期:InvalidArgumentException,实际:invoked method),代码看起来像这样:
@Test
public void wrongInput() {
Throwable exception = assertThrows(InvalidArgumentException.class,
()->{objectName.yourMethod("WRONG");} );
}
您可以使用assertThrows()。我的例子取自文档http://junit.org/junit5/docs/current/user-guide/
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
....
@Test
void exceptionTesting() {
Throwable exception = assertThrows(IllegalArgumentException.class, () -> {
throw new IllegalArgumentException("a message");
});
assertEquals("a message", exception.getMessage());
}
在Java 8和JUnit 5 (Jupiter)中,我们可以这样断言异常。 使用org.junit.jupiter.api.Assertions.assertThrows
public static < T extends Throwable > T assertThrows(类< T > expectedType, 可执行可执行) 断言所提供的可执行文件的执行将抛出expectedType的异常并返回该异常。 如果没有抛出异常,或者抛出了不同类型的异常,则此方法将失败。 如果您不想对异常实例执行额外的检查,只需忽略返回值。
@Test
public void itShouldThrowNullPointerExceptionWhenBlahBlah() {
assertThrows(NullPointerException.class,
()->{
//do whatever you want to do here
//ex : objectName.thisMethodShoulThrowNullPointerExceptionForNullParameter(null);
});
}
该方法将使用org.junit.jupiter.api中的函数接口可执行文件。
参考:
http://junit.org/junit5/docs/current/user-guide/#writing-tests-assertions http://junit.org/junit5/docs/5.0.0-M2/api/org/junit/jupiter/api/Executable.html http://junit.org/junit5/docs/5.0.0-M4/api/org/junit/jupiter/api/Assertions.html#assertThrows-java.lang.Class-org.junit.jupiter.api.function.Executable-