Use the HAMCRES
Use the HAMCRES
Introduction:
Hamcrest is a framework for writing a more readable and maintainable test that asserts the code.It provides an elegant way to make a high -level assertion of collection and objects.This article will introduce to you how to use the HAMCREST framework for advanced assertions and objects, and provide some Java code examples.
1. Introduce the Hamcrest framework
First, we need to introduce the dependencies of the Hamcrest framework in the project.It can be introduced by Maven or manual download, and the specific dependence is as follows:
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest</artifactId>
<version>2.2</version>
</dependency>
Second, gather assertion
It is very intuitive and easy to use to use the HAMCREST framework for the collection assertion.Here are several commonly used collection assertions:
1. Determine whether the set is empty:
import static org.hamcrest.Matchers.empty;
import java.util.ArrayList;
import java.util.List;
import org.hamcrest.MatcherAssert;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.*;
List<String> list = new ArrayList<>();
MatcherAssert.assertThat(list, empty());
2. Judgment the size of the set:
List<Integer> numbers = List.of(1, 2, 3, 4, 5);
MatcherAssert.assertThat(numbers, hasSize(5));
3. Determine whether the set contains specific elements:
List<String> fruits = List.of("apple", "banana", "orange");
MatcherAssert.assertThat(fruits, contains("apple", "banana", "orange"));
4. Determine whether the elements in the collection meet the specific conditions:
List<Integer> numbers = List.of(1, 2, 3, 4, 5);
MatcherAssert.assertThat(numbers, everyItem(greaterThan(0)));
Third, object assertion
The HAMCREST framework can also be used for object assertions. Here are some commonly used object assertive examples:
1. Determine whether the object is a specific type:
import static org.hamcrest.Matchers.instanceOf;
import static org.hamcrest.MatcherAssert.assertThat;
Object obj = "Hello World";
MatcherAssert.assertThat(obj, instanceOf(String.class));
2. Determine whether the attribute value of the object meets the specific conditions:
import static org.hamcrest.Matchers.*;
import static org.hamcrest.MatcherAssert.*;
class Person {
private String name;
private int age;
// omit the getter and setter method
@Override
public String toString() {
return "Person [name=" + name + ", age=" + age + "]";
}
}
Person person = new Person("John", 30);
MatcherAssert.assertThat(person, allOf(
hasProperty("name", equalTo("John")),
hasProperty("age", greaterThan(20))
));
Fourth, summary
By using the HAMCREST framework, we can write more readable and maintainable assertions.In terms of set assertions, we can assert whether the collection is empty, the collection size, the specific elements, and all elements meet the specific conditions.In terms of object assertions, we can determine whether the object type and object attribute value meets specific conditions.
By using the HAMCREST framework, we can improve the readability and maintenance of test cases, making our test code more concise and elegant.I hope this article will help you understand and use the Hamcrest framework.