EQUALSVERIFIER | Detailed explanation of Java -class library technical principles of the Release Normal Jar framework
EQUALSVERIFIER is a framework for the Java class library that can help developers quickly and accurately verify whether classes meet the contracts of Equals and Hashcode.This article will explain the technical principles of the EqualSverifier framework in detail and provide some Java code examples.
Introduction to EQUALSVERIFIER
EQUALSVERIFIER aims to simplify the unit testing of the Java class and provide a way to automate the EQUALS and HashCode methods correctly.The main principle is to test the reflex mechanism and randomly generated objects.
Second, equalsverifier basic usage methods
Before using EQUALSVERIFIER, we need to import the Equalsverifier library and add it to the project's dependence.We can then test whether our Java class is correctly implemented using Equals and Hashcode methods with EqualSverifier.
The following is an example of the basic usage method of Equalsverifier:
import nl.jqno.equalsverifier.EqualsVerifier;
public class MyClassTest {
@Test
public void testEqualsAndHashCode() {
EqualsVerifier.forClass(MyClass.class).verify();
}
}
In this example, we first introduced the EQUALSVERIFIER library and used the `Forclass ()` method to specify the test class to be tested.We then call the `Verify () method to perform the test.If the Equals and HashCode methods are implemented correctly, the test will pass, otherwise the error will be thrown.
Third, EQUALSVERIFIER technical principles
The technical principles of EQUALSVERIFIER are to test the correctness of Equals and Hashcode methods by combining reflexes and random generation objects.Specific steps are as follows:
1. Obtain all fields and methods to test the class through reflection.
2. For each field, create two instances by randomly generated objects, and verify whether the results of its Equals and Hashcode methods meet the expectations.
3. Check whether all non -Final fields in the class are used in Equals and HashCode methods.
4. Check whether the Equals and HashCode methods meet a series of contract requirements, such as reflection, symmetry, transmission, and consistency.
5. Check whether the Equals method is properly handled the NULL value.
Fourth, EQUALSVERIFIER
EQUALSVERIFIER also provides many advanced usage to test the Equals and HashCode methods that are more flexible.Here are some examples of advanced usage:
1. Eliminate field: Use the method to use the method of `excludefields ()` to exclude that the specific field is not tested by the EQUALSVERIFIER.
2. Customized generation object: Use the `Withprefabvalues () method to specify the customized objects to test the Equals and HashCode methods in specific conditions.
3. Ignore inheritance: Use the method of using `withonlythesefields ()` `method can ignore the parent class field, and only pay attention to the current fields of the current class during testing.
The following is an example code for advanced usage:
import nl.jqno.equalsverifier.EqualsVerifier;
public class MyClassTest {
@Test
public void testEqualsAndHashCode() {
EqualsVerifier.forClass(MyClass.class)
.excludeFields("fieldToExclude")
.withPrefabValues(CustomObject.class, new CustomObject("value1"), new CustomObject("value2"))
.withOnlyTheseFields("fieldName1", "fieldName2")
.verify();
}
}
In this example, we use the method called "Fieldtoexclude" with `Excludefields ()` method.Then, we set two predetermined values of the CustomObject class with the method of `` WithprefabValues () `to test for testing in specific conditions.Finally, use the method we want to test using the method we want to test with the method of using `` Withonlythesefields () `method.
5. Summary
EQUALSVERIFIER is a convenient framework for Java libraries, which can help developers automatically test the correctness of Equals and HashCode methods.It verifies whether the implementation of the class can meet the contract requirements by combining the reflex mechanism and the randomly generated object.By using the advanced usage of EQUALSVERIFIER, developers can more flexibly test Equals and HashCode methods in different scenarios.