11

I tried the Unitils tool which is really great, but doesn't provide you an option for excluding fields. The only way by doing this is to set null the objects and enable the IGNORE_DEFAULTS flag, but in my case, it's not helpful, since I've got some ids autogenerated by the system. So if I could just add the id to an exclude list it would be perfect.

I also tried Mockito's ReflectionEquals but it was not helpful, since I need a field-to-field comparison.

Is there something else helpful? I have been searching for hours without success.

Thank you in advance!

Dmitriy Popov
  • 2,150
  • 3
  • 25
  • 34
Harter Oliver
  • 111
  • 1
  • 1
  • 3
  • Side note: if you end up writing your own "reflection based" field checker ... consider using **assertThat** and writing your own custom matchers for it. That might at least allow you to make use of the superior features of **assertThat**. – GhostCat Jun 08 '16 at 08:20

3 Answers3

24

You could solve your problem by using AssertJ. It can do field-by-field recursive comparison with options to ignore fields by name, regex or type.

assertThat(sherlock)
  .usingRecursiveComparison()
  .ignoringFields("name", "home.address.street")
  .isEqualTo(moriarty);

bfncs
  • 10,007
  • 4
  • 32
  • 52
Frank Neblung
  • 3,047
  • 17
  • 34
-1

It wouldn't be that hard to roll your own using java reflection, if you don't like the capabilities of the ones you're finding.

Joseph Larson
  • 8,530
  • 1
  • 19
  • 36
  • Saying "do it yourself if you don't like what you've got so far" is not in any way helping the OP solve the problem – Sparm Jul 09 '21 at 11:11
  • 5 years ago... I find if I spend more than about a half hour searching for a tool that will do what I want, then having someone say, "Maybe you can roll your own with " can be a useful nudge. He spent hours searching and isn't satisfied. He also never gave a "best answer" green checkbox to the suggestion of AssertJ. Maybe someone nudging java reflection at him was useful or not. Maybe for other people who see this answer, a nudge at reflection may be enough to look and consider. Or maybe not. – Joseph Larson Jul 09 '21 at 17:11
-1

We use our own approach in groovy (similar to Frank Neblung's answer ) where we convert an object to a formated Json string using an ObjectMapper and a filterList for the attributes. This is pretty handy for testing but has also some downsides like the loss of type information.

// interface of the conversion
String nice(Object o, String... filterList) { ... }

// test calls
assertEquals(nice(actual, ["-unusedAttribute", "-unusedAttribute2"]), nice(expected, ["-unusedAttribute", "-unusedAttribute2"]))
assertEquals(nice(actual, ["+usedAttibute", "+usedAttibute2"]), nice(expected, ["+usedAttibute", "+usedAttibute2"]))

// test calls using the spock framework
nice(actual, ["-unusedAttribute", "-unusedAttribute2"]) == nice(expected, ["-unusedAttribute", "-unusedAttribute2"])
nice(actual, ["+usedAttibute", "+usedAttibute2"]) == nice(expected, ["+usedAttibute", "+usedAttibute2"])
Community
  • 1
  • 1
RonaldFindling
  • 332
  • 2
  • 12