1. Overview
1.概述
In this tutorial, we’ll take a look at the AssertJ library, especially at defining and using conditions to create readable and maintainable tests.
在本教程中,我们将看看AssertJ库,特别是在定义和使用条件来创建可读和可维护的测试。
AssertJ basics can be found here.
AssertJ的基础知识可以在这里找到。
2. Class Under Test
2.被测类
Let’s have a look at the target class against which we’ll write test cases:
让我们来看看我们要编写测试用例的目标类。
public class Member {
private String name;
private int age;
// constructors and getters
}
3. Creating Conditions
3.创造条件
We can define an assertion condition by simply instantiating the Condition class with appropriate arguments.
我们可以通过简单地实例化带有适当参数的Condition类来定义一个断言条件。
The most convenient way to create a Condition is to use the constructor that takes a Predicate as a parameter. Other constructors require us to create a subclass and override the matches method, which is less handy.
创建Condition最方便的方法是使用以Predicate为参数的构造函数。其他构造函数要求我们创建一个子类并重写matches方法,这就不太方便了。
When constructing a Condition object, we must specify a type argument, which is the type of the value against which the condition is evaluated.
当构造一个Condition对象时,我们必须指定一个类型参数,即条件被评估的值的类型。
Let’s declare a condition for the age field of our Member class:
让我们为我们的Member类的age字段声明一个条件。
Condition<Member> senior = new Condition<>(
m -> m.getAge() >= 60, "senior");
The senior variable now references a Condition instance which tests if a Person is senior based on its age.
现在,senior变量引用了一个Condition实例,该实例根据Person的age测试其是否为senior。
The second argument to the constructor, the String “senior”, is a short description that will be used by AssertJ itself to build a user-friendly error message if the condition fails.
构造函数的第二个参数,即字符串 “高级”,是一个简短的描述,如果条件失败,AssertJ本身将使用它来建立一个用户友好的错误信息。
Another condition, checking whether a Person has the name “John”, looks like this:
另一个条件是检查一个Person是否有name“John”,看起来像这样。
Condition<Member> nameJohn = new Condition<>(
m -> m.getName().equalsIgnoreCase("John"),
"name John"
);
4. Test Cases
4.测试案例
Now, let’s see how to make use of Condition objects in our test class. Assume that the conditions senior and nameJohn are available as fields in our test class.
现在,让我们看看如何在我们的测试类中使用Condition对象。假设条件senior和nameJohn在我们的测试类中作为字段可用。
4.1. Asserting Scalar Values
4.1.断言标量值
The following test should pass as the age value is above the seniority threshold:
由于年龄值高于资历门槛,以下测试应该通过。
Member member = new Member("John", 65);
assertThat(member).is(senior);
Since the assertion with the is method passes, an assertion using isNot with the same argument will fail:
由于使用is方法的断言通过了,使用isNot的相同参数的断言将失败。
// assertion fails with an error message containing "not to be <senior>"
assertThat(member).isNot(senior);
Using the nameJohn variable, we can write two similar tests:
使用nameJohn变量,我们可以写两个类似的测试。
Member member = new Member("Jane", 60);
assertThat(member).doesNotHave(nameJohn);
// assertion fails with an error message containing "to have:\n <name John>"
assertThat(member).has(nameJohn);
The is and has methods, as well as the isNot and doesNotHave methods have the same semantics. Which we use is just a matter of choice. Nevertheless, it is recommended to pick the one that makes our test code more readable.
is和has方法,以及isNot和doesNotHave方法的语义相同。我们使用哪个只是一个选择问题。尽管如此,建议选择能使我们的测试代码更易读的那一种。
4.2. Asserting Collections
4.2.断言集合
Conditions don’t work only with scalar values, but they can also verify the existence or non-existence of elements in a collection. Let’s take a look at a test case:
条件并不仅仅适用于标量值,它们也可以验证集合中元素的存在与否。让我们看看一个测试案例。
List<Member> members = new ArrayList<>();
members.add(new Member("Alice", 50));
members.add(new Member("Bob", 60));
assertThat(members).haveExactly(1, senior);
assertThat(members).doNotHave(nameJohn);
The haveExactly method asserts the exact number of elements meeting the given Condition, while the doNotHave method checks for the absence of elements.
haveExactly方法断定符合给定Condition的元素的确切数量,而doNotHave方法检查没有元素。
The methods haveExactly and doNotHave are not the only ones working with collection conditions. For a complete list of those methods, see the AbstractIterableAssert class in the API documentation.
haveExactly 和 doNotHave 方法不是唯一与集合条件有关的方法。有关这些方法的完整列表,请参阅 API 文档中的 AbstractIterableAssert 类。
4.3. Combining Conditions
4.3.合并条件
We can combine various conditions using three static methods of the Assertions class:
我们可以使用Assertions类的三个静态方法来组合各种条件:。
- not – creates a condition that is met if the specified condition is not met
- allOf – creates a condition that is met only if all of the specified conditions are met
- anyOf – creates a condition that is met if at least one of the specified conditions is met
Here’s how the not and allOf methods can be used to combine conditions:
下面是如何使用not和allOf方法来组合条件。
Member john = new Member("John", 60);
Member jane = new Member("Jane", 50);
assertThat(john).is(allOf(senior, nameJohn));
assertThat(jane).is(allOf(not(nameJohn), not(senior)));
Similarly, we can make use of anyOf:
同样地,我们可以利用anyOf。
Member john = new Member("John", 50);
Member jane = new Member("Jane", 60);
assertThat(john).is(anyOf(senior, nameJohn));
assertThat(jane).is(anyOf(nameJohn, senior));
5. Conclusion
5.结论
This tutorial gave a guide to AssertJ conditions and how to use them to create very readable assertions in your test code.
本教程给出了AssertJ条件的指南,以及如何使用它们在你的测试代码中创建非常可读的断言。
The implementation of all the examples and code snippets can be found over on GitHub.
所有例子和代码片段的实现都可以在GitHub上找到over。