Compare maps ignoring given fields

倖福魔咒の 提交于 2021-02-17 06:22:28

问题


I feel like I'm missing something obvious here, but the documentation is letting me down. I'm attempting to compare two maps while ignoring a set of fields in assertJ. I would like to see this assert pass:

  private static final String[] IGNORED_FIELDS = { "ignored", "another" };
  private static final Map<String, Object> TEST_PAYLOAD = ImmutableMap.of("test", "payload", "example", "value", "ignored", "field");
  private static final Map<String, Object> COMPARISON_PAYLOAD = ImmutableMap.of("test", "payload", "example", "value", "another", "ignored field");
  // assert fails
  assertThat(TEST_PAYLOAD).isEqualToIgnoringGivenFields(COMPARISON_PAYLOAD, IGNORED_FIELDS);

However, the comparison that actually occurs is of the map objects, and fails on things like size, modCount, threshold, etc. In addition, it doesn't actually ignore the fields listed when comparing tables, keys, and values. I have also tried using

  assertThat(TEST_PAYLOAD).usingRecursiveComparison().ignoringGivenFields(IGNORED_FIELDS).isEqualTo(COMPARISON_PAYLOAD);

but this failed because it attempted to compare the ignored fields. Is there an elegant solution here, or am I going to have to manually iterate through keys?


回答1:


ignoringGivenFields() won't work, because it's an ObjectAssert, not a MapAssert method and operates on object's properties, not map's keys, as you pointed out.

That said, I believe there's no built-in AssertJ method which you could use, but you can write your own filter method and apply it before doing equality test:

private static <V> Map<String, V> filterIgnoredKeys(Map<String, V> map) {
    return Maps.filterKeys(map, key -> !IGNORED_FIELDS.contains(key));
}
// later
assertThat(filterIgnoredKeys(TEST_PAYLOAD))
        .isEqualTo(filterIgnoredKeys(COMPARISON_PAYLOAD))

If you want the solution to be more elegant, you can experiment with your own custom assertion.




回答2:


The testPayload actually contains these key-value pairs:

"test" -> "payload", 
"example" -> "value", 
"ignored" -> "field"

Comparison payload has these key-value pairs:

"test" -> "payload", 
"example" -> "value", 
"another" -> "ignored field"

That is, you need to apply .ignoringGivenFields(IGNORED_FIELDS) to both payloads to succeed. In your first attempt you removed "another" from comparison, but "ignored" remained in the test payload; and vice versa in the second attempt.



来源:https://stackoverflow.com/questions/61641396/compare-maps-ignoring-given-fields

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!