Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug in equals tester #248

Open
p91paul opened this issue Jun 25, 2020 · 0 comments · May be fixed by #249
Open

Bug in equals tester #248

p91paul opened this issue Jun 25, 2020 · 0 comments · May be fixed by #249

Comments

@p91paul
Copy link

p91paul commented Jun 25, 2020

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);
p91paul added a commit to p91paul/pojo-tester that referenced this issue Jun 25, 2020
@p91paul p91paul linked a pull request Jun 25, 2020 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant