HAMCREST framework introduction and application

HAMCREST framework introduction and application introduction: The HAMCREST framework is a test library suitable for Java language. It provides an elegant way to write unit testing and integration testing.HAMCREST introduced a scalable matching gallery that can be used to verify whether the object meets the expected conditions.This article will introduce the basic concepts and usage methods of the Hamcrest framework, and provide some Java code examples to help readers understand. 1. HAMCREST framework overview: The core concept of the Hamcrest framework is Matcher.Matcher is an object that implements the Matcher interface, which provides a mechanism to verify whether the object meets the expected conditions.The HAMCREST framework provides some of the general matchmakers that have been defined, such as Equalto, Greaterthan, Lessthan, etc., and also supports custom matching device. 2. Step of the HAMCREST framework: (1) Import the HAMCREST library: Before using the HAMCREST framework in the Java project, you need to add the Hamcrest library to the dependence of the project.Can be imported by Maven or manually downloading jar package. (2) Create a matching device: According to specific test needs, use the existing matching or custom matching matching device provided by Hamcrest to create one or more Matcher objects. (3) Abstract and verification: Use the Assertthat syntax provided by Hamcrest to match the test object with the matching device and determine whether the test is passed based on the matching results. 3. Common use scenarios of HAMCREST framework: (1) Collection matching: In testing, we often need to verify whether a set contains specific elements and whether there are specific sizes.Use HAMCREST setting matching comparators can more easily implement these functions. Example code: import org.hamcrest.MatcherAssert; import org.hamcrest.collection.IsCollectionWithSize; import org.hamcrest.core.IsIterableContaining; import org.junit.Test; import java.util.Arrays; import java.util.List; public class CollectionMatcherTest { @Test public void testCollectionMatcher() { List<String> colors = Arrays.asList("Red", "Green", "Blue"); MatcherAssert.assertThat(colors, IsCollectionWithSize.hasSize(3)); MatcherAssert.assertThat(colors, IsIterableContaining.hasItem("Red")); } } (2) Abnormal matching: In the test, sometimes it is necessary to verify whether a method will throw a specific abnormalities. The abnormal matching of the Hamcrest can easily assert. Example code: import org.hamcrest.MatcherAssert; import org.hamcrest.core.IsInstanceOf; import org.junit.Test; public class ExceptionMatcherTest { @Test public void testExceptionMatcher() { String str = null; try { str.length(); } catch (Exception e) { MatcherAssert.assertThat(e, IsInstanceOf.instanceOf(NullPointerException.class)); } } } 4. Custom matchmaker: In some cases, the existing matches provided by Hamcrest cannot meet the test needs. At this time, a matcher can be customized.Custom matrix needs to implement the MATCHER interface and rewrite the method. Example code: import org.hamcrest.BaseMatcher; import org.hamcrest.Description; import org.hamcrest.Matcher; import org.junit.Test; public class CustomMatcherTest { @Test public void testCustomMatcher() { String str = "Hello, World!"; Matcher<String> customMatcher = new CustomMatcher(); MatcherAssert.assertThat(str, customMatcher); } private static class CustomMatcher extends BaseMatcher<String> { @Override public boolean matches(Object item) { String str = (String) item; return str.contains("Hello"); } @Override public void describeTo(Description description) { description.appendText("should contain \"Hello\""); } } } Summarize: Through the introduction of this article, we understand the basic concepts and usage methods of the Hamcrest framework.The HAMCREST framework can help developers more conveniently write unit testing and integrated testing by providing a flexible and scalable matching gallery, and improve the readability and maintenance of the test code. references: - Hamcrest. (2021). Retrieved from https://hamcrest.org/