sta-szek / pojo-tester

Java testing framework for testing pojo methods. It tests equals, hashCode, toString, getters, setters, constructors and whatever you report in issues ;)
http://www.pojo.pl
GNU Lesser General Public License v3.0
53 stars 26 forks source link

Bug in equals tester #248

Open p91paul opened 4 years ago

p91paul commented 4 years ago

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);