Search code examples
javajunitassertj

AssertJ: issue with Assertions.assertThat().usingRecursiveComparison().ignoringFields()


I'm using assertj-core:3.21.0 and JDK 17

Here is an simple example that fails. This works in JDK 16 and assertj-core:3.19.0.

        @AllArgsConstructor
        @Data
        class Test {
            @JsonIgnore
            private final ObjectMapper objectMapper = new ObjectMapper();

            private String name;
        }

        Assertions.assertThat(Map.of(
            1, new Test("a"),
            2, new Test("b")
        )).usingRecursiveComparison().ignoringFields("objectMapper").isEqualTo(Map.of(
            1, new Test("a"),
            2, new Test("b")
        ));

The error I'm getting is java.lang.reflect.InaccessibleObjectException: Unable to make field private volatile java.lang.Object java.util.concurrent.atomic.AtomicReference.value accessible: module java.base does not "opens java.util.concurrent.atomic" to unnamed module @48140564

I found if I remove private final ObjectMapper objectMapper = new ObjectMapper(); from the class it will work, so looks like ignoringFields did not ignore objectMapper properly.

Any idea?


Solution

  • That probably works pre java 17, see How to solve InaccessibleObjectException ("Unable to make {member} accessible: module {A} does not 'opens {package}' to {B}") on Java 9?

    I'm raising an issue in AssertJ recursive comparison not to use reflexion to compare AtomicReference.

    Done: https://github.com/assertj/assertj-core/issues/2466