EQUALSVERIFIER | Advanced Usage Tutorial for Release Normal Jar framework
EQUALSVERIFIER is a tool library for testing the Java object, which can help developers write high -quality test cases.This article will introduce the advanced usage of Equalsverifier to help readers better understand and use the framework.
1. Introduction
EQUALSVERIFIER is an open source Java library that is used to verify and test the correctness of the method of `Equals () and` HashCode () `.By using EQUALSVERIFIER, developers can easily verify the equivalent of the object when writing test cases and capture possible errors.This tool library can help developers avoid common `Equals () and` HashCode () `to achieve errors, and improve the quality and reliability of code.
Second, use Equalsverifier
1. Introduce dependencies
Before using EQUALSVERIFIER, you need to introduce the dependencies of EQUALSVERIFIER in the configuration file of the project.In the Maven project, you can add the following dependencies to the `pom.xml` file of the project:
<dependency>
<groupId>nl.jqno.equalsverifier</groupId>
<artifactId>equalsverifier</artifactId>
<version>3.4.2</version>
<scope>test</scope>
</dependency>
2. Write test cases
When writing test cases, by creating a new test class, and using Equalsverifier's `Forclass ()` method to specify the type of object type to be tested.For example, for a class called `Person`, you can write the following test cases:
import nl.jqno.equalsverifier.EqualsVerifier;
public class PersonTest {
@Test
public void testEqualsAndHashCode() {
EqualsVerifier.forClass(Person.class).verify();
}
}
3. Run test case
When the test case is handled, the EqualSverifier will automatically test the correctness of the method of `Equals () and` Hashcode () `.If you find any errors or inconsistencies, it will throw the corresponding abnormalities and prompt developers to repair it.
3. Advanced usage
In addition to basic usage, Equalsverifier also provides many advanced functions to help developers better test object equivalent testing.Here are some examples of high -level usage:
1. Eliminate fields
Sometimes, certain fields may not require the equivalent comparison of the object.You can use the method of `` WithignoredFields () `to eliminate these fields.For example:
EqualsVerifier.forClass(Person.class)
.withIgnoredFields("age")
.verify();
2. Eliminate type
In some scenarios, if the type of the object changes, you can use the method to eliminate type examinations with the method of `` WithReadefinEdsuperClass () `.For example:
EqualsVerifier.forClass(Person.class)
.withRedefinedSuperclass()
.verify();
3. Verify constructor
Using Equalsverifier can also verify whether the constructor of the object is correct.Verify through the method of `VerifyConStructor ()`.For example:
EqualsVerifier.forClass(Person.class)
.verifyConstructor();
4. Junit 5 integration
For projects using Junit 5, you can use the `@parameterizedTest` and@Methodsource` to achieve parameterized testing.For example:
import org.junit.jupiter.api.extension.ExtensionContext;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;
public class PersonTest {
@ParameterizedTest
@MethodSource("personProvider")
public void testEqualsAndHashCode(Person person) {
EqualsVerifier.forClass(Person.class).verify();
}
public static Stream<Person> personProvider() {
return Stream.of(new Person("Alice", 20), new Person("Bob", 25));
}
}
Fourth, summary
This article introduces the advanced usage of Equalsverifier to help readers better understand and use the framework.By using EQUALSVERIFIER, developers can easily test the equivalent of the object and capture potential errors.Hope this article will help you be tested in using Equalsverifier for testing.