Skip to content

ComparingOnlyFieldsOfType only provided types for comparison #3207

@timmisset

Description

@timmisset

Describe the bug
Assuming that this is not intentionally designed I chose bug, but it might very well be a feature request to further tweak the recursiveComparison behavior:

When comparing a nested structure the types provided in the comparingOnlyFieldsOfTypes not only affect the comparison but also the collection of values to compare.

  • assertj core version: 3.24.2
  • java version: 17
  • test framework version: junit 5.9.2

Test case reproducing the bug

The following test case will pass. However, when I use (NestedClass.class, String.class) as arguments for the comparingOnlyFieldsOfTypes the assertion will fail.
As said, this is probably by design but would be really great is if there is an option that the collection of values to compare does not filter out the NestedClass so that "A" and "B" are still collected to be compared.

I have a nested tree where a specific class occurs at different locations and I only want to compare the identifier field. This would require me to put all branches in the comparison to make sure they are reached.

@Test
    void test() {
        TestClass a = new TestClass(new NestedClass("A"));
        TestClass b = new TestClass(new NestedClass("B"));

        assertThat(a).usingRecursiveComparison()
                .comparingOnlyFieldsOfTypes(String.class)
                .isEqualTo(b);
    }

    @RequiredArgsConstructor
    private class TestClass {
        private final NestedClass nestedClass;
    }

    @RequiredArgsConstructor
    private class NestedClass {
        private final String nestedClassField;
    }

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions