1. Overview
1.概述
When writing unit tests, we sometimes provide multiple assertions of the output. The test stops when the first of these assertions fails. This means that we don’t find out if any of the later assertions would have passed or failed, which can increase debugging time.
在编写单元测试时,我们有时会提供多个输出的断言。当这些断言中的第一个失败时,测试就会停止。这意味着我们不会发现后面的断言是否通过或失败,这可能会增加调试时间。
We can solve this problem by wrapping multiple assertions up into a single action.
我们可以通过将多个断言包装成一个动作来解决这个问题。
In this short tutorial, we’ll learn how to use the assertAll() method introduced in JUnit5 and see how it’s different from using multiple assertions.
在这个简短的教程中,我们将学习如何使用JUnit5中介绍的assertAll()方法,看看它与使用多个断言有什么不同。
2. Model
2.模型
We’ll use a User class to help with our examples:
我们将使用一个User类来帮助我们的例子。
public class User {
String username;
String email;
boolean activated;
//constructors
//getters and setters
}
3. Using Multiple Assertions
3.使用多个断言
Let’s start with an example where all of our assertions would fail:
让我们从一个例子开始,我们所有的断言都会失败。
User user = new User("baeldung", "support@baeldung.com", false);
assertEquals("admin", user.getUsername(), "Username should be admin");
assertEquals("admin@baeldung.com", user.getEmail(), "Email should be admin@baeldung.com");
assertTrue(user.getActivated(), "User should be activated");
After running the test only the first assertion fails:
运行测试后,只有第一个断言失败。
org.opentest4j.AssertionFailedError: Username should be admin ==>
Expected :admin
Actual :baeldung
Let’s say we fix the failing code or test and re-run the test. We’d then get a second failure, and so on. It would be better, in this situation, to group all these assertions into a single pass/failure.
假设我们修复了失败的代码或测试并重新运行测试。然后我们会得到第二次失败,以此类推。在这种情况下,最好将所有这些断言归入一个单一的通过/失败中。
4. Using the assertAll() Method
4.使用assertAll()方法
We can group assertions with JUnit5 using assertAll().
我们可以用JUnit5使用 assertAll()来分组断言。
4.1. Understanding assertAll()
4.1 了解assertAll()
The assertAll() assertion function takes a collection of multiple Executable objects:
assertAll() assertion函数接收一个多个Executable对象的集合。
assertAll(
"Grouped Assertions of User",
() -> assertEquals("baeldung", user.getUsername(), "Username should be baeldung"),
// more assertions
...
);
We can therefore use lambdas to provide each of our assertions. The lambda will be called to run the assertion within the grouping provided by assertAll().
因此,我们可以使用lambdas来提供我们的每个断言。lambda将被调用以在assertAll()提供的分组内运行断言。
Here, in the first parameter to assertAll(), we’ve also provided a description to explain the meaning of the whole group.
在这里,在assertAll()的第一个参数中,我们还提供了一个描述来解释整个组的含义。
4.2. Grouping Assertions Using assertAll()
4.2.使用assertAll()对断言进行分组
Let’s see the complete example:
让我们看看完整的例子。
User user = new User("baeldung", "support@baeldung.com", false);
assertAll(
"Grouped Assertions of User",
() -> assertEquals("admin", user.getUsername(), "Username should be admin"),
() -> assertEquals("admin@baeldung.com", user.getEmail(), "Email should be admin@baeldung.com"),
() -> assertTrue(user.getActivated(), "User should be activated")
);
Now, let’s see what happens when we run the test:
现在,让我们看看当我们运行测试时会发生什么。
org.opentest4j.MultipleFailuresError: Grouped Assertions of User (3 failures)
org.opentest4j.AssertionFailedError: Username should be admin ==> expected: <admin> but was: <baeldung>
org.opentest4j.AssertionFailedError: Email should be admin@baeldung.com ==> expected: <admin@baeldung.com> but was: <support@baeldung.com>
org.opentest4j.AssertionFailedError: User should be activated ==> expected: <true> but was: <false>
Contrary to what happened with multiple assertions, this time all the assertions were executed and their failures were reported in the MultipleFailuresError message.
与多个断言的情况相反,这次所有的断言都被执行了,它们的失败被报告在MultipleFailuresError消息中。
We should note that assertAll() only handles AssertionError. If any assertion were to end with an exception, rather than the usual AssertionError, execution stops immediately and the error output would relate to the exception, rather than MultipleFailuresError.
我们应该注意,assertAll()只处理AssertionError。如果任何断言以异常结束,而不是通常的AssertionError,执行将立即停止,错误输出将与异常有关,而不是MultipleFailuresError。
5. Conclusion
5.总结
In this article, we learned to use assertAll() in JUnit5 and saw how it’s different than using multiple individual assertions.
在这篇文章中,我们学习了在JUnit5中使用assertAll(),并看到它与使用多个单独的断言有什么不同。
As always, the complete code for the tutorial is available over on GitHub.
一如既往,该教程的完整代码可在GitHub上获取。