1. Overview
1.概述
In this tutorial, we’ll explore how to use JUnit fail assertion for common testing scenarios.
在本教程中,我们将探讨如何将JUnit fail断言用于常见的测试场景。
We’ll also see fail() method differences between JUnit 4 and JUnit 5.
我们还将看到 fail() 方法在JUnit 4和JUnit 5之间的差异。
2. Using fail Assertion
2.使用fail断言
The fail assertion fails a test throwing an AssertionError unconditionally.
fail断言使测试失败,无条件地抛出一个AssertionError。
When writing unit tests, we can use fail to explicitly create a failure under desired testing conditions. Let’s see some cases where this can be helpful.
在编写单元测试时,我们可以使用fail来明确地在所需的测试条件下创建一个失败。让我们看看在哪些情况下这是有用的。
2.1. Incomplete Test
2.1.不完整的测试
We can fail a test when it is incomplete or not yet implemented:
当一个测试不完整或尚未实施时,我们可以不通过该测试。
@Test
public void incompleteTest() {
fail("Not yet implemented");
}
2.2. Expected Exception
2.2.预期的例外情况
We can also do it when we think an exception will happen:
当我们认为会发生异常时,我们也可以这样做。
@Test
public void expectedException() {
try {
methodThrowsException();
fail("Expected exception was not thrown");
} catch (Exception e) {
assertNotNull(e);
}
}
2.3. Unexpected Exception
2.3.意外的例外情况
Failing the test when an exception is not expected to be thrown is another option:
当没有预期的异常被抛出时,失败的测试是另一种选择。
@Test
public void unexpectedException() {
try {
safeMethod();
// more testing code
} catch (Exception e) {
fail("Unexpected exception was thrown");
}
}
2.4. Testing Condition
2.4.测试条件
We can call fail() when a result doesn’t meet some desired condition:
当一个结果不符合某些预期条件时,我们可以调用fail() 。
@Test
public void testingCondition() {
int result = randomInteger();
if(result > Integer.MAX_VALUE) {
fail("Result cannot exceed integer max value");
}
// more testing code
}
2.5. Returning Before
2.5.返回之前
Finally, we can fail a test when the code doesn’t return/break when expected:
最后,当代码没有在预期时返回/中断时,我们可以不通过测试。
@Test
public void returnBefore() {
int value = randomInteger();
for (int i = 0; i < 5; i++) {
// returns when (value + i) is an even number
if ((i + value) % 2 == 0) {
return;
}
}
fail("Should have returned before");
}
3. JUnit 5 vs JUnit 4
3.JUnit 5 vs JUnit 4
All assertions in JUnit 4 are part of org.junit.Assert class. For JUnit 5 these were moved to org.junit.jupiter.api.Assertions.
JUnit 4中的所有断言都是org.junit.Assert类的一部分。对于JUnit 5,这些都被移到org.junit.jupiter.api.Assertions.。
When we call fail in JUnit 5 and get an exception, we receive an AssertionFailedError instead of AssertionError found in JUnit 4.
当我们在JUnit 5中调用fail并得到一个异常时,我们会收到一个AssertionFailedError,而不是在JUnit 4中发现的AssertionError。
Along with fail() and fail(String message), JUnit 5 includes some useful overloads:
除了fail()和fail(String message)之外,JUnit 5还包括一些有用的重载。
- fail(Throwable cause)
- fail(String message, Throwable cause)
- fail(Supplier<String> messageSupplier)
In addition, all forms of fail are declared as public static <V> V fail() in JUnit 5. The generic return type V, allows these methods to be used as single-statement in lambda expressions:
此外,fail的所有形式在JUnit 5中被声明为public static <V> V fail()。通用的返回类型V,允许这些方法在lambda表达式中作为单句使用:。
Stream.of().map(entry -> fail("should not be called"));
4. Conclusion
4.总结
In this article, we covered some practical use cases for the fail assertion in JUnit. See JUnit Assertions for all available assertions in JUnit 4 and JUnit 5.
在这篇文章中,我们介绍了JUnit中fail断言的一些实际使用案例。请参阅JUnit 断言,了解JUnit 4 和 JUnit 5 中的所有可用断言。
We also highlighted the main differences between JUnit 4 and JUnit 5, and some useful enhancements of the fail method.
我们还强调了JUnit 4和JUnit 5之间的主要区别,以及fail方法的一些有用的改进。
As always, the full source code of the article is available over on GitHub.
一如既往,文章的完整源代码可在GitHub上获得。