[Solved] Java equals does not working correctly


Using the built in equals for this kind of test is a bad idea for a couple of reasons:

  1. As you can see here, a test failure doesn’t give you the diagnostics that you want to actually figure out why the objects are different
  2. If you’re implementing equals purely for the test, if it changes later on (usually for a business reason) then your test might be passing now but it shouldn’t
  3. You need to keep the equals method up to date as you add/remove fields.

I can recommend using the sameBeanAs method that we wrote that will do a reflective deep object comparison and provide you good diagnostics at the same time. You can use it like this:

assertThat(p, sameBeanAs(personService.findById(10)));

See here for information on how to use and access it

solved Java equals does not working correctly