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

Make pojo test fails when equals and hashcode have unwanted fields #121

Closed
jerckov opened this Issue Sep 29, 2016 · 1 comment

Comments

Projects
None yet
2 participants
@jerckov

jerckov commented Sep 29, 2016

I have f.e. class Person which should be identified only by id field:

public class Person {

   private final UUID id;
   private final String name;

   public Person(final String name) {
         this.id = UUID.randomUUID();
         this.name = name;
   }

   @Override
    public boolean equals(final Object obj) {
        if (this == obj) {
            return true;
        }

        if (obj == null || getClass() != obj.getClass()) {
            return false;
        }

        final Person person = (Person) obj;

        return new EqualsBuilder().append(id, person.id)
                                  .isEquals();
    }

    @Override
    public int hashCode() {
        return new HashCodeBuilder().append(id)
                                    .toHashCode();
    }
}

... and I use PojoTester on two ways to check the equals and hashCode methods:

    @Test
    public void firstWayPojoTesterTest() {
        // given
        final Predicate<String> included = FieldPredicate.include("id");

        // when
        final Class<Person> clazz = Person.class;

        // then
        assertPojoMethodsFor(clazz, included).testing(Method.EQUALS, Method.HASH_CODE)
                                             .areWellImplemented();
    }

    @Test
    public void secondWayPojoTesterTest() {
        // given
        final Predicate<String> excluded = FieldPredicate.exclude("name");

        // when
        final Class<Person> clazz = Person.class;

        // then
        assertPojoMethodsFor(clazz, excluded).testing(Method.EQUALS, Method.HASH_CODE)
                                             .areWellImplemented();
    }

If I or someone else make a mistake on class Person and change f.e. equals method to

...
    @Override
    public boolean equals(final Object obj) {
        if (this == obj) {
            return true;
        }

        if (obj == null || getClass() != obj.getClass()) {
            return false;
        }

        final Person person = (Person) obj;

        return new EqualsBuilder().append(id, person.id)
                                  .append(name, person.name) // culprit!!!
                                  .isEquals();
    }
...

then above test are pass but should fails.

@sta-szek

This comment has been minimized.

Owner

sta-szek commented Sep 29, 2016

Actually, tests of equals and hashCode methods are performed as follows:

  1. Gnerator generates different objects based on fields which you specified.
  2. Compares those objects and check that they are not equal.

We have to improve this feature to generate objects based on fields you excluded and then check that those objects are still equal, beacuse those fields should not be included in hashCode and equals methods implementation.

@sta-szek sta-szek changed the title from PojoTester pass the tests when additional fields are added to Make pojo test fails when equals and hashcode have unwanted fields Sep 29, 2016

@sta-szek sta-szek added this to the 0.6.0 milestone Oct 3, 2016

@sta-szek sta-szek self-assigned this Oct 19, 2016

sta-szek pushed a commit that referenced this issue Oct 19, 2016

sta-szek pushed a commit that referenced this issue Oct 19, 2016

sta-szek pushed a commit that referenced this issue Oct 19, 2016

Piotr Joński

sta-szek pushed a commit that referenced this issue Oct 19, 2016

Piotr Joński

sta-szek pushed a commit that referenced this issue Oct 19, 2016

Piotr Joński
Merge pull request #139 from sta-szek/#121-Make-pojo-test-fails-when-…
…equals-and-hashcode-have-unwanted-fields

#121 make pojo test fails when equals and hashcode have unwanted fields

@sta-szek sta-szek closed this Oct 19, 2016

@sta-szek sta-szek removed the in progress label Oct 19, 2016

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment