spring-bootjacksonspring-testspring-test-mvc

ObjectMapper doesn't map JSON's String to ZonedDateTime


I am writing Integration tests with MockMvc and want to receive my Response as an Object in order to make assertions on it.

The line

AuthenticationResponse authenticationResponse = objectMapper.readValue(mvcResult.getResponse().getContentAsString(), AuthenticationResponse.class);

Throws the error: Unable to make field private final java.time.ZoneOffset java.time.ZonedDateTime.offset accessible: module java.base does not "opens java.time" to unnamed module

There are no errors using my application usually. There is only an Error parsing it in the Test.

Searching Stackoverflow didn't help as there are no similar questions.


Solution

  • You need to run your tests with the following flag:

    java ... --add-opens java.base/java.time=ALL-UNNAMED
    

    See also: https://github.com/spring-projects/spring-data-mongodb/issues/3893