Skip to content

Add default recursive comparison configuration for non accessible classes and fields #3369

@Meijuh

Description

@Meijuh

Feature summary

At this moment I do not think AssertJ supports non-accessible classes and fields with Java 17+. Would it be useful to have a default RecursiveComparisonConfiguration that ignores all non exported packages, and non-accessible fields?

Example

public class ClassUnderTest {
    private final int foo;

    private final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
    
    public ClassUnderTest(int foo) {
        this.foo = foo;
    }
    
    public static ClassUnderTest classUnderTest() {
        return new ClassUnderTest(42);
    }
}

And the test class as follows, note the configuration for the recursive comparison.

class ClassUnderTestTest {

    @Test
    void testClassUnderTest() {
        assertThat(ClassUnderTest.classUnderTest())
            .usingRecursiveComparison().ignoreNonExportedPackages()
            .isEqualTo(new ClassUnderTest(42));
    }
}

In the above, I'm only interested in the fact that I correctly call the constructor with value 42. Suppose this class is part of a library, then I'm unable to add an equals() and hashcode().
Without the call to (the proposed method) .ignoreNonExportedPackages() the test would fail. Currently, I would have to write .ignoringFieldsOfTypes(ReentrantReadWriteLock.class). In general I would need to ignore much more types and fields, since those would always cause errors in my test. Similar to non exported packages, one could also add a configuration; .ignoreNonAccessibleFields().

I'm interested to hear your opinion.

Metadata

Metadata

Labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions