Use Hamcrest Library to guide the Java class library test
Use the HAMCREST Library to guide the Java class library test
Introduction:
Hamcrest is a Java class library used to write better testing assertions.It provides a set of matches that can be used to write clear, flexible and scalable assertions.The design goal of the HAMCREST library is to easily read and understand the code of testing the test assertion, thereby improving the readability and maintenance of test cases.
The core idea of the HAMCREST library is to expect the test expression to be correct in the natural language.It asserts the real value of a certain expression based on the use of a matching device, not just an equivalent or unknown comparison.With the Hamcrest library, you can write an assertion with natural language style, making the test code easier to read and understand.
The steps of using the HAMCREST library for the Java library test are as follows:
Step 1: Add Hamcrest to the project
First, we need to add the dependencies of the HAMCREST library to the project.You can add related dependencies in the project construction tool (such as Maven or Gradle).
For Maven project, you can add the following dependencies to the pom.xml file of the project:
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-library</artifactId>
<version>2.2</version>
<scope>test</scope>
</dependency>
Step 2: Import the necessary class
In the test class, the relevant class in the Hamcrest library needs to be introduced.According to the need, you can import different HAMCREST classes to use the matching device it provided.
For example, you can import `ORG.hamcrest.Matcher` classes to use the standard matcher of Hamcrest.
import static org.hamcrest.MatcherAssert.assertThat;
Step 3: Use the Hamcrest matching device to assert
The core of using the Hamcrest library is to use the matching device to write an assertion.You can use the HAMCREST matching class to perform various assertions, such as confirming whether a certain value is matched with the expected value, whether the set contains a certain element, and the judgment is abnormal.
Here are some examples of assertion using the HAMCREST matching device:
Example 1: Determine whether the two values are equal
import static org.hamcrest.Matchers.equalTo;
int actualValue = calculateSomething();
int expectedValue = 10;
assertThat(actualValue, equalTo(expectedValue));
Example 2: Determine whether the set contains a certain element
import static org.hamcrest.Matchers.contains;
List<String> actualList = getListOfValues();
String expectedValue = "example";
assertThat(actualList, contains(expectedValue));
Example 3: Determine whether it is thrown abnormal
import static org.hamcrest.Matchers.anything;
import static org.hamcrest.Matchers.instanceOf;
void performOperation() throws SomeException {
// Perform some operation
}
assertThat(() -> performOperation(), throwsException(anything()));
Summarize:
Using the HAMCREST library for the Java library test can make the test code more readable and maintained.By using natural language style assertions and flexible matches, you can write a clear and scalable test assertion.By following the above steps, you can start using the Hamcrest library to write higher -quality test cases.