Skip to content

ignoringFields on recursive comparison no longer works with maps with null values #3722

@grantaveryatgfs

Description

@grantaveryatgfs

Describe the bug

As part of solving #2988, 835c1dc changed how maps were checked for equality with a new filterIgnoredFields function. This function uses Java's Collectors.toMap to convert a stream back to a map, which internally uses uniqKeysMapAccumulator, which throws a NullPointerException if any map entry values are null.

This is not ideal for a testing framework, which should be designed to handle all kinds of possible map scenarios, including ones with null entry values. Please replace this toMap call (deeplink) with a different function to convert to a map which allows for null values, for example this one suggested in Stack Overflow: https://stackoverflow.com/questions/24630963/nullpointerexception-in-collectors-tomap-with-null-entry-values/32648397#32648397. Thanks!

  • assertj core version: 3.27.0
  • java version: 21

Test case reproducing the bug

  @Test
  public void assertj_ignoringFieldsWithNullValue_shouldNotThrow() {
    Map<String, String> actualMap = new HashMap<>();
    actualMap.put("exampleKey", "exampleValue");
    actualMap.put("nullKey", null);

    Map<String, String> expectedMap = new HashMap<>();
    expectedMap.put("exampleKey", "exampleValue");
    expectedMap.put("nullKey", null);

    assertThat(actualMap)
      .usingRecursiveComparison()
      .ignoringFields("exampleKey")
      .isEqualTo(expectedMap);
  }

Relates to:

Metadata

Metadata

Assignees

Labels

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions