0

I'm currently struggling with how to make sure 2 maps have the same key/values in a unit-test, while ignoring some of the keys. This should be applied recursively, as a value within the map might be a map again - so that I want to check the whole tree.

Maybe an example is helpful here:

Map<String, Object> mapA = Map.of("foo", "bar", "description", "foobar", "submap", Map.of("subFoo", "subBar", "description", "subFooBar"));
Map<String, Object> mapB = Map.of("foo", "bar", "description", "barfoo", "submap", Map.of("subFoo", "subBar", "description", "subBarFoo"));

This 2 maps should be considered equal in regard to the test-result.

I tried to solve this by the following approach - but unfortunately it does not work'

assertThat(mapA)
        .usingRecursiveComparison()
        .ignoringFieldsMatchingRegexes(".*description")
        .ignoringFields("description")
        .isEqualTo(mapB);

Does anyone have an idea how to solve that?

digital-h
  • 301
  • 3
  • 11
  • 1
    Does this answer your question? [Compare maps ignoring given fields](https://stackoverflow.com/questions/61641396/compare-maps-ignoring-given-fields) – Amongalen Jan 19 '21 at 10:20
  • Unfortunately not, as this is not filtering on a deeper-level, but only the top-level-keys. – digital-h Jan 19 '21 at 11:04

0 Answers0