
Automatic tester for equals(), hashCode(), compareTo(), and toString() methods of a class.
Similar to Guava's EqualsTester, but designed for Kotlin Multiplatform with enhanced validation features.
Example
testEquality {
group(User("alex"), User("alex"), User("alex"))
group(User("sergey"), User("sergey"))
group { User("page") }
}
- Each group must contain objects that are equal to each other, but not equal to objects in any other group.
- Adding more than one group is optional, but highly recommended.
- There is no limit on the number of groups or objects. If the total number of test iterations is below
maxIterations, all combinations are tested exhaustively. Otherwise, randomized testing is used to keep execution time reasonable.
Setup
implementation("io.github.adokky:equals-tester:1.1.0")
This tests
Basic Equality Checks
- An object is equal to itself.
- Comparing an object to
null returns false.
- Comparing an object to an instance of an incompatible class returns
false.
Comparable Checks
- A
Comparable object compared to itself returns 0.
- Objects within the same group return
0 when compared.
- Objects from different groups return non-zero values.
- Ensures consistent ordering across all equality groups.
Collection Contract Checks
For objects implementing List, Map, or Set:
- Verifies that the hash code matches the value defined by the collection contract.
- Ensures iteration over elements produces a collection equal to the original.
toString Checks
If checkToString is enabled:
- Objects within the same group have equal
toString() results.
- Objects from different groups have different
toString() results.
API Overview