JUnit 4 和 5 中断言抛出异常
最后更新:2025 年 3 月 26 日
1. 简介
在本快速教程中,我们将探讨如何测试是否抛出异常以及如何测试是否未抛出异常,使用 JUnit 库。
当然,我们将涵盖 JUnit 4 和 JUnit 5 的版本。
2. JUnit 5
让我们看看 JUnit 5 中的异常处理断言。
2.1. 断言抛出异常
JUnit 5 Jupiter 断言 API 引入了 assertThrows 方法来断言异常。
它接受期望异常的类型和一个 Executable 函数式接口,我们可以在其中通过 lambda 表达式传递被测代码
@Test
void whenExceptionThrown_thenAssertionSucceeds() {
Exception exception = assertThrows(NumberFormatException.class, () -> {
Integer.parseInt("1a");
});
String expectedMessage = "For input string";
String actualMessage = exception.getMessage();
assertTrue(actualMessage.contains(expectedMessage));
}
如果抛出了预期的异常,assertThrows 会返回该异常,这使我们还可以断言消息。
此外,重要的是要注意,**当封闭的代码抛出 NumberFormatException 类型或其任何派生类型的异常时,此断言才成立。**
这意味着如果我们将 Exception 作为预期的异常类型传递,则任何抛出的异常都将使断言成功,因为 Exception 是所有异常的超类型。
如果我们将上面的测试更改为期望 RuntimeException,这也会通过
@Test
void whenDerivedExceptionThrown_thenAssertionSucceeds() {
Exception exception = assertThrows(RuntimeException.class, () -> {
Integer.parseInt("1a");
});
String expectedMessage = "For input string";
String actualMessage = exception.getMessage();
assertTrue(actualMessage.contains(expectedMessage));
}
assertThrows() 方法为异常断言逻辑提供了更细粒度的控制,因为我们可以在代码的特定部分周围使用它。
2.2. 断言未抛出异常
有时,确保代码块或方法在不抛出任何异常的情况下执行非常重要。JUnit 5 提供了一种简单的方法来执行此检查。让我们看一个例子
@Test
void givenABlock_whenExecutes_thenEnsureNoExceptionThrown {
assertDoesNotThrow(() -> {
Integer.parseInt("100");
});
}
assertDoesNotThrow() 方法执行提供的代码块。如果代码块未抛出异常,则测试通过。如果抛出异常,则测试失败。
2.3. 断言未抛出特定类型的异常
在某些情况下,我们可能需要断言代码不会导致特定类型的异常。JUnit 没有为此提供内置方法。但是,我们可以编写一个自定义方法来处理此场景
首先,让我们创建一个 函数式接口,允许我们泛型地定义可以在自定义实现中使用接口。
@FunctionalInterface
public interface Executable {
void execute() throws Exception;
}
然后,我们可以将其用于自定义实现,以传递所需的异常类并执行代码块
private <T extends Exception> void assertSpecificExceptionIsNotThrown(Class<T> exceptionClass, Executable executable) {
try {
executable.execute();
} catch (Exception e) {
if (exceptionClass.isInstance(e)) {
fail(e.getClass().getSimpleName() + " was thrown");
} else {
// Any other exception types are ignored and test passes!
// Logging it here for debugging purpose
LOG.info("Caught exception: " + e.getClass().getName() + ", but ignoring since it it not an instance of " + exceptionClass.getName())
}
}
}
现在,我们可以将此方法用作
@Test
void givenASpecificExceptionType_whenBlockExecutes_thenEnsureThatExceptionIsNotThrown() {
assertSpecificExceptionIsNotThrown(IllegalArgumentException.class, () -> {
int i = 100 / 0;
});
}
如果代码块抛出 IllegalArgumentException 或其任何子类型,则此测试将失败。如果未生成任何其他类型的异常或异常,则测试将通过。这在我们需要确保从不抛出特定类型的异常的场景中很有用。
3. JUnit 4
在本节中,让我们看看 JUnit 4 中的不同异常处理断言。
3.1. 断言抛出异常
在使用 JUnit 4 时,我们可以简单地 使用 @Test 注解的 expected 属性 来声明我们期望在被注解的测试方法中的任何位置抛出异常。
因此,当运行测试时,如果未抛出指定的异常,它将失败,如果抛出,则将通过
@Test(expected = NullPointerException.class)
public void whenExceptionThrown_thenExpectationSatisfied() {
String test = null;
test.length();
}
在此示例中,我们声明我们的测试代码将导致 NullPointerException。
如果仅对断言抛出异常感兴趣,这就足够了。
当我们需要验证异常的其他一些属性时,我们可以使用 ExpectedException 规则。
让我们看一个验证异常的 message 属性的例子
@Rule
public ExpectedException exceptionRule = ExpectedException.none();
@Test
public void whenExceptionThrown_thenRuleIsApplied() {
exceptionRule.expect(NumberFormatException.class);
exceptionRule.expectMessage("For input string");
Integer.parseInt("1a");
}
在上面的例子中,我们首先声明 ExpectedException 规则。然后在我们的测试中,我们断言尝试解析 Integer 值的代码将导致带有消息“For input string”的 NumberFormatException。
3.2. 断言未抛出异常
与 JUnit 5 不同,JUnit 4 没有内置方法来断言代码不会生成异常。然而,我们可以很容易地实现这个逻辑。让我们看看实现方法
private void assertNoExceptionIsThrown(Executable executable) {
try {
executable.execute();
} catch (Exception e) {
fail(e.getClass().getSimpleName() + " was thrown");
}
}
我们利用之前创建的 Executable 函数接口来传递代码块。如果抛出任何异常,我们捕获它们并显式地使测试失败。现在,我们可以将它用在测试中
@Test
public void givenABlock_whenExecuted_thenEnsureThatNoExceptionAreThrown() {
assertNoExceptionIsThrown(() -> {
int d = 100 / 10;
});
}
如果代码块中抛出任何异常,测试将失败。
4. 结论
在本文中,我们介绍了使用 JUnit 4 和 JUnit 5 断言异常的方法。我们检查了断言抛出异常的方法,以及确保不抛出异常的方法。此外,我们创建了一个自定义实现来处理特定类型的异常。
支持本文的代码可在 GitHub 上获取。 一旦你以 Baeldung Pro 会员 身份登录,就开始学习并在项目上进行编码。















