EQUALSVERIFIER | Best Practice Suggestions of the Release Normal Jar framework
EQUALSVERIFIER | Best Practice Suggestions of the Release Normal Jar framework
Overview:
EQUALSVERIFIER is a library for testing the Java class.When using EQUALSVERIFIER, we don't need to write a large number of test code to verify whether the equal methods of the class are correctly implemented.It provides a simple and powerful way to verify the equivalent of the class, thereby avoiding the tedious process of test cases.This article will introduce the best practical suggestions of Equalsverifier and provide relevant Java code examples.
1. Introduce EQUALSVERIFIER:
Before starting to use EQUALSVERIFIER, you need to introduce the relevant libraries of Equalsverifier into the project.Add the following dependencies to our pom.xml file:
<dependencies>
<dependency>
<groupId>nl.jqno.equalsverifier</groupId>
<artifactId>equalsverifier</artifactId>
<version>3.3</version>
</dependency>
</dependencies>
2. Basic usage:
The basic usage of Equalsverifier is very simple.For classes to test equal nature, we only need to use EQUALSVERIFIER in the test method for testing.The following is a basic example:
import nl.jqno.equalsverifier.EqualsVerifier;
import org.junit.Test;
public class MyClassTest {
@Test
public void testEquals() {
EqualsVerifier.forClass(MyClass.class).verify();
}
}
In the above example, we pass the class to Equalsverifier through the `Forclass` method and call the` Verify` method to perform equal testing.
3. Build a custom equivalent class:
If the class contains interfaces, abstracts, or private member variables, Equalsverifier may not be processed correctly.In this case, we need to build a custom equivalent category for this type.The following is an example:
import nl.jqno.equalsverifier.EqualsVerifier;
import org.junit.Test;
public class MyClassTest {
@Test
public void testEquals() {
EqualsVerifier.forClass(MyClass.class)
.withPrefabValues(MyInterface.class, new MyInterfaceImpl(1), new MyInterfaceImpl(2))
.verify();
}
}
In the above example, we use the `` Withprefabvalues "method to build a custom equivalent class as the interface` myInterface`.This is very important for EqualSverifier to correctly verify that the class or other complex types containing interfaces or other complex types are very important.
4. Eliminate the field:
In some cases, you may want to exclude some fields that do not participate in equal testing.To achieve this, we can use the `Excludefields` method to eliminate these fields.The following is an example:
import nl.jqno.equalsverifier.EqualsVerifier;
import org.junit.Test;
public class MyClassTest {
@Test
public void testEquals() {
EqualsVerifier.forClass(MyClass.class)
.excludeFields("field1", "field2")
.verify();
}
}
In the above examples, we exclude the fields called `Field1` and` Field2` through the method of `Excludefields` to ensure that they do not participate in equal testing.
in conclusion:
Using the Equalsverifier Library can easily test the same, and help us avoid writing lengthy test code.This article introduces the basic usage of Equalsverifier, and provides some best practical suggestions and example code.I hope this information can help you perform equal testing more efficiently when using Equalsverifier.