The basic principles and implementation methods of the Hamcrest Integration framework in the Java class library
Hamcrest Integration is an integrated library that uses the Hamcrest framework in the Java library.It provides a simple way to use the Hamcrest assertion library to achieve a more expressive test assertion.
Before understanding the basic principles of Hamcrest Integration, let's first understand the Hamcrest framework itself.HAMCREST is a framework for writing more readability statements.It provides a rich matching library, so that we can write an assertive sentence more naturally.Compared with the traditional Junit, Hamcrest's syntax is more expressive, and can meet more complicated more compared needs by custom MATCher.
The basic principle of HAMCREST INTEGRATIN is to integrate Hamcrest's matcher into the Java library for use in testing.It provides a series of MATCHER factory methods for various data types, so that we can directly use HAMCREST asserting grammar to write test assertions.
Below is an example code using Hamcrest Integration:
import org.hamcrest.Matcher;
import static org.hamcrest.MatcherAssert.assertThat;
public class MyExample {
public static void main(String[] args) {
String str = "Hello, World!";
assertThat(str, containsString("Hello"));
}
public static Matcher<String> containsString(String substring) {
return new CustomContainsStringMatcher(substring);
}
}
class CustomContainsStringMatcher extends TypeSafeMatcher<String> {
private final String substring;
public CustomContainsStringMatcher(String substring) {
this.substring = substring;
}
@Override
protected boolean matchesSafely(String item) {
return item.contains(substring);
}
@Override
public void describeTo(Description description) {
description.appendText("a string containing ").appendValue(substring);
}
}
In the above example, we use the HAMCREST Integration library to use the custom Matcher to assert whether a string contains a sub -string.In the `Main` method, we used the` Containsstring` factory method to create a match and pass it to the `Assertthat` method for assertions.
According to the configuration of the above code, we need to ensure that the project has been introduced in the project. The Hamcrest Integration library is introduced. In the Maven project, the following dependencies can be added to the POM.XML file:
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest</artifactId>
<version>2.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-library</artifactId>
<version>2.2</version>
<scope>test</scope>
</dependency>
These dependencies will be imported into Hamcrest and Hamcrest Library libraries so that we can use Hamcrest Integration in the project.
In summary, Hamcrest Integration is a library that integrates the Hamcrest framework into the Java library, which simplifies the process of using Hamcrest to assert.By providing the MATCHER factory method, we can directly use HAMCREST asserting grammar to write test assertions.By introducing the dependency library of Hamcrest Integet and according to the relevant configuration, we can easily use the Hamcrest Integration in the project for more expressive test assertions.