CompareVerifier is a pure Java library that can be used in Junit tests to verify that classes implementing Comparable interfaces are defined correctly.
By default verification includes:
- compareTo being consistent with equals
- compareTo failing on a null argument
- satisfying
sgn(a.compareTo(b)) == -sgn(b.compareTo(a)) - satisfying
sgn(a.compareTo(c)) == sgn(b.compareTo(c)) => sgn(a.compareTo(b)) == 0 - satisfying
sgn(a.compareTo(b)) > 0 && sgn(b.compareTo(c)) > 0 => sgn(a.compareTo(c)) > 0
final VerificationInstancesCreator<BigDecimal> lesserCreator =
VerificationInstancesCreators.from(
new BigDecimal("0.0"), // smaller then the objects returned by
new BigDecimal("1.0"), // both equal creator and greater creator
new BigDecimal("2.0")
);
final VerificationInstancesCreator<BigDecimal> equalCreator =
VerificationInstancesCreators.from(
new BigDecimal("42.0"),
new BigDecimal("42.0")
);
final VerificationInstancesCreator<BigDecimal> greaterCreator =
VerificationInstancesCreators.from(
new BigDecimal("101.0"), // larger then the objects returned by
new BigDecimal("202.0"), // both equal creator and lesser creator
new BigDecimal("303.0")
);
ComparableVerifier
.<Foo>forInstances(lesserCreator, equalCreator, greaterCreator)
.verify(); Where:
lessInstancesCreatorcreates instance of the class implementingComparablethat are less then instances created byequalInstancesCreatorequalInstancesCreatorcreates instances that should be equal to each other, they should be greater then those created bylessInstancesCreatorand less then those created bygreaterInstancesCreatorgreaterInstancesCreatorcreates instances that should be greater then those created by the two other creators
It's possible to disable one or more of the default verification checks by using set of "suppress" methods.
ComparableVerifier
.<Foo>forInstances(lesserCreator, equalCreator, greaterCreator)
.suppressConsistentWithEquals(true)
.suppressExceptionOnCompareToNull(true)
.verify(); You can use this library in conjunction with JUnit 4.12. There are two ways of getting the library:
- through releases
- through the use of JITPACK.io
To configure your project with JITPACK you need to in your build.gradle file at the end of repositories section add this line:
allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}Add ComparatorVerifier to the list of your test dependencies:
dependencies {
testImplementation 'com.github.artificialrevelations:CompareVerifier:master-SNAPSHOT'
}- detailed usage examples