Frequently Asked Questions of EQUALSVERIFIER | Release Normal Jar framework
Frequently Asked Questions of EQUALSVERIFIER | Release Normal Jar framework
EQUALSVERIFIER is an open source framework to help developers check and verify the Equals and HashCode methods that Java classes.This article will provide answers to the common questions of the EQUALSVERIFIER framework and provide some Java code examples.
Question 1: How to use Equalsverifier?
Answer: It is very simple to use Equalsverifier.Just follow the steps below for operation:
1. Import the library dependencies of Equalsverifier.
2. Create an instance of a test class and define the test of the test.
3. Use the EQUALSVERIFER.FORCLASS method to create an EQUALSVERIFIER instance and pass it into the class to be tested.
4. Call the Verify method of the EQUALSVERIFIR instance to verify whether the implementation of the Equals and HashCode methods is correct.
The following is an example:
import nl.jqno.equalsverifier.EqualsVerifier;
public class MyClassTest {
@Test
public void testEqualsAndHashCode() {
MyClass myObject = new MyClass("example");
EqualsVerifier.forClass(MyClass.class)
.verify();
}
}
Question 2: How to exclude field verification?
Answer: In some cases, you may need to exclude some fields that do not participate in the verification of Equals and HashCode methods.You can use the EQUALSVERIFER.EXCLUDEFIELDS method to implement.The following is an example:
import nl.jqno.equalsverifier.EqualsVerifier;
import nl.jqno.equalsverifier.Warning;
public class MyClassTest {
@Test
public void testEqualsAndHashCode() {
MyClass myObject = new MyClass("example");
EqualsVerifier.forClass(MyClass.class)
.excludeFields("fieldToExclude")
.verify();
}
}
Question 3: How to customize verification rules?
Answer: Equalsverifier allows you to verify using customized verification rules.You can use EQUALSVERIFIFIIFIPREFABVALUES and EQUALSVERIFIFIER.WITHREDEFINEDSUPERCLASS method.The following is an example:
import nl.jqno.equalsverifier.EqualsVerifier;
import nl.jqno.equalsverifier.Warning;
public class MyClassTest {
@Test
public void testEqualsAndHashCode() {
MyClass myObject = new MyClass("example");
EqualsVerifier.forClass(MyClass.class)
.withPrefabValues(OtherClass.class, new OtherClass("example1"), new OtherClass("example2"))
.withRedefinedSuperclass()
.suppress(Warning.STRICT_INHERITANCE)
.verify();
}
}
Question 4: How to deal with unavailable class Equals and HashCode methods?
Answer: Equalsverifier provides special support for the verification of Equals and HashCode methods of unavailable classes.You can use the EQUALSVERIFER.FORCLASS method and pass into immutable.class to verify the non -changing class.The following is an example:
import nl.jqno.equalsverifier.EqualsVerifier;
public class ImmutableClassTest {
@Test
public void testEqualsAndHashCode() {
ImmutableClass myObject = new ImmutableClass("example");
EqualsVerifier.forClass(ImmutableClass.class)
.verify();
}
}
I hope the above common questions can help you better understand and use the EQUALSVERIFIER framework.You can further understand the more functions and usage of the framework by reading the documentation and example code of Equalsverifier.