How to write custom HAMCRES
How to write custom HAMCRES
Overview:
Hamcrest is a framework for writing simple, readable assertions, which provides many built -in matching devices.However, in some cases, we may need to write a custom HAMCREST matching device to meet specific test needs.This article will introduce how to write a custom HAMCREST matching device and how to use them in the Java code.
step:
Here are the steps to write a custom HAMCREST matching device:
1. Create a new Java class and implement ORG.hamcrest.Matcher interface.The interface defines the behavior and method of the matching device.
2. In the implementation Matcher interface, at least the following three methods need to be rewritten:
-Matches (Object item): Check whether the given object is matched with the matching conditions.
-DESCRIBETO (Description Description): Describe the behavior of the matching device to generate error information.
-DESCRIBEMISMATCH (Object item, Descripting MismatchDescription): The reason why the description is not matched when the match fails.
Below is an example of a custom HAMCREST matcher, which is used to check whether the length of the strings meet the requirements:
import org.hamcrest.Description;
import org.hamcrest.Matcher;
import org.hamcrest.TypeSafeMatcher;
public class StringLengthMatcher extends TypeSafeMatcher<String> {
private final int expectedLength;
public StringLengthMatcher(int expectedLength) {
this.expectedLength = expectedLength;
}
// Check whether the length of the given string is equal to the expected value
@Override
protected boolean matchesSafely(String item) {
return item.length() == expectedLength;
}
// Describe the behavior of the matchinger
@Override
public void describeTo(Description description) {
description.appendText("a string with length of " + expectedLength);
}
// The reason why the description is not matched
@Override
protected void describeMismatchSafely(String item, Description mismatchDescription) {
mismatchDescription.appendText("was a string with length of " + item.length());
}
// Create a static factory method for the custom matching device to simplify the use
public static Matcher<String> hasLength(int expectedLength) {
return new StringLengthMatcher(expectedLength);
}
}
In the above example, we created a custom matching match called `StringLengthMatcher`, which is used to check whether the length of the string is equal to the length of the given.First of all, we inherited the `TypesaFematcher` class, which provides a safe type of inspection.Then, we rewritten the `MatchesSaFly` method, which is used to check whether the given string length is equal to the expectation value.Next, we rewritten the `Describeto` method and the method of` DescribeMISMATCHSAFly` to describe the behavior and non -matching reasons of the matchmaker.Finally, we created a `Haslength` static factory method to simplify the use of the custom matching device in the code.
Example usage: The following is a sample code using a custom matching device:
import org.junit.Assert;
import org.junit.Test;
import static example.StringLengthMatcher.hasLength;
public class StringLengthMatcherTest {
@Test
public void testStringLengthMatcher() {
String str = "Hello";
// Use a custom matching device to assert
Assert.assertThat(str, hasLength(5));
}
}
In the above example, we use a custom matchinger `haslength` to check whether the length of the string` str` is 5.The assertion will pass, because the length of the string `Hello" `is 5.
in conclusion:
By writing a custom HAMCREST matching device, we can determine whether the test results meet the expectations according to specific needs.The writing steps of the custom matching device include implementing the `Matcher` interface, and rewrite the method of` Matches`, `Descripto` and` Descriptionsmatic.Using a custom matching device can make the assertive sentence more clear and readable.