pojo-tester
pojo-tester copied to clipboard
Bug in equals tester
I wrote the following, wrong class; pojo tester claims it's well implemented.
public class TestClass {
@Nullable
private Double testField;
public TestClass(@Nullable Double testField) {
this.testField = testField;
}
@Nullable
public Double getTestField() {
return testField;
}
public void setTestField(@Nullable Double testField) {
this.testField = testField;
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
TestClass that = (TestClass)o;
if (!(testField == null && that.testField == null) || (testField != null && that.testField != null && Double.compare(testField, that.testField) != 0)) {
return false;
}
return true;
}
@Override
public int hashCode() {
return Objects.hash(testField);
}
@Override
public String toString() {
final StringBuilder sb = new StringBuilder("TestClass {");
sb.append("testField=").append(testField);
sb.append('}');
return sb.toString();
}
}
However, this simple test is enough to show the equals method is wrong:
TestClass testClass = new TestClass(2d);
TestClass testClass2 = new TestClass(2d);
assertEquals(testClass, testClass2);