Quickly get started Hamcrest Library: Basic usage in the Java class library test

Quickly get started Hamcrest Library: Basic usage in the Java class library test Overview: In Java development, testing is a key link that helps us verify the correctness of the code and improve the quality of the software.During the test, we often need to verify whether certain conditions are established, such as whether the return value of a method meets expected.In order to test it more conveniently, the Hamcrest library came into being.HAMCREST is an assertion library for writing concise, easy -to -read, that is, inserted and used, which provides a flexible set of matt (Matcher) for conditional judgments. Matcher's basic concept: In Hamcrest, Matcher is the basic unit that determines whether the result is in line with expected.Matcher is responsible for comparing with the actual results. It can accept a actual value and return a Boer type result.By using Matcher, we can decompose the test into a series of clear rules, making the test code easier to read and maintain. Matcher's basic usage: First of all, we need to introduce the dependencies of the Hamcrest library and introduce the relevant class: import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.*; Then we can start using Matcher for testing.Here are some commonly used Matcher and its usage examples: 1. EQUALTO () -Keving the actual values and expected values are equal assertThat(42, equalTo(42)); assertThat("Hello", equalTo("Hello")); 2. IS () -eyn of Equalto () assertThat("Hello", is(equalTo("Hello"))); 3. Not () -Keval that the actual value is not equal to the expected value assertThat(42, not(equalTo(0))); assertThat("Hello", not(equalTo("World"))); 4. NullValue () -Drocketing the actual value is NULL assertThat(null, nullValue()); 5. NotnullValue () -W verifies the actual value of non -NULL assertThat("Hello", notNullValue()); 6. Containsstring () -Adividing string contains a specified sub -string assertThat("Hello World", containsString("World")); 7. Startswith () -Keval string starts with the specified prefix assertThat("Hello World", startsWith("Hello")); 8. ENDSWITH () -Ad verification string ends with a specified suffix assertThat("Hello World", endsWith("World")); 9. Hasitem () -The verification collection contains the specified element List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5); assertThat(numbers, hasItem(3)); 10. hassize () -Drive the size of the set List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5); assertThat(numbers, hasSize(5)); These are just some of the commonly used MATCHER in the Hamcrest library. In fact, Hamcrest also provides many other matcher, which can be selected as needed. Summarize: By using the Hamcrest library, we can write test code more concise and intuitive.Matcher's flexibility makes the judgment of the test conditions more accurate, which greatly improves the readability and maintenance of the test code.I hope this article will help you quickly get started with the Hamcrest library and be applied in your Java class library test.