In -depth understanding of Hamcrest Library: Introduction to the common matching in the Java class library
In -depth understanding of Hamcrest Library: Introduction to the common matching in the Java class library
Introduction:
HAMCREST is a matching gallery widely used in the Java test.It provides developers with a powerful and flexible matching device that can be used to write an assertion that is easy to read and maintain, and write a custom matching device.This article will introduce the common matching of the Hamcrest library and its usage, and provide some Java code example for reference.
1. Introduction to Hamcrest
Hamcrest is an open source project developed by Joe Walnes and Nat Pryce. It aims to improve the readability and maintenance of the assertion in the Java test.HAMCREST provides a scalable matching library to help developers write customized assertions and improve the readability of test code.The HAMCREST matching can be used for various types of assertions, including basic types, sets, objects, etc.
2. Introduction to Common Micksters
1. Equalto
Equal matching device is used to compare whether the two objects are equal.It accepts an expected value as a parameter and uses it to compare it in an assertion.If the actual value is equal to the expected value, the assertion is successful.
Code example:
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
int expected = 5;
int actual = 5;
assertThat(actual, equalTo(expected));
2. Interval matching (Greaterthan, Lessthan)
The interval matcher is used to compare whether a value is greater than or less than a given expectation value.It accepts an expected value as a parameter and uses it to compare it in an assertion.If the actual value is greater than (or less than) the expected value, the assertion is successful.
Code example:
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.greaterThan;
import static org.hamcrest.Matchers.lessThan;
int expected = 10;
int actual = 15;
assertThat(actual, greaterThan(expected));
assertThat(actual, lessThan(expected));
3. Contains the matching (containsstring)
Contains the matching device to check whether the string contains the specified sub -string.It accepts an expected sub -string as a parameter and uses it in an assertion to check.If the string contains the sub -string, the assertion is successful.
Code example:
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsString;
String expected = "world";
String actual = "Hello world!";
assertThat(actual, containsString(expected));
4. Collection matchmaker
The collection matching device is used to assert the set.The Hassize matchinger is used to check whether the size of the set is equal to the expected value.Containsinanyorder matching device is used to check whether the set of elements contains a given element, regardless of the order of the element.
Code example:
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.Matchers.containsInAnyOrder;
List<String> names = Arrays.asList("Alice", "Bob", "Charlie");
assertThat(names, hasSize(3));
assertThat(names, containsInAnyOrder("Charlie", "Alice", "Bob"));
5. Custom matches (CUSTMMATCHER)
HAMCREST also allows developers to create custom matchingrs to meet specific assertions.Custom matrix needs to implement the MATCHER interface and provide methods such as matching logic and readability description.
Code example:
import org.hamcrest.Description;
import org.hamcrest.TypeSafeMatcher;
public class CustomMatcher extends TypeSafeMatcher<String> {
protected boolean matchesSafely(String item) {
// matching logic
}
public void describeTo(Description description) {
Descripting.appendtext ("Customizing the Description Information of the Edition");
}
}
When using a custom matching device, you can assess the use of the Matcherassert.Assertthat method, similar to the usage of other built -in matrix.
3. Summary
This article introduces the concept of a commonly used matching library HAMCREST and the usage of a common matching device in the Java class library.By using the HAMCREST library, developers can write a readability and maintenance assertion, and customize the matching device to meet specific test needs.It is hoped that this article can help readers more deeply understand how to use the Hamcrest library for Java testing.
(Note: The above example code is only the purpose of demonstration, not a complete and running code. When actual use, please adjust accordingly according to the specific situation.)