DateTimeParseException could not be parsed at index 20 - MockMvc test

Viewed 9

I have an API that is sending some OffsetDateTimes in this format:

"startTime": "2019-08-17T07:20:06.303Z"

and this formatDateTime in place:

public OffsetDateTime formatDateTime(String dateTime) {
        return OffsetDateTime.parse(dateTime, DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.000Z"), OffsetDateTime.);
    }

So in my test if I call the API with MockMvc:

mockMvc.perform( MockMvcRequestBuilders
                        .post("/config")
                        .content(asJsonString(request))
                        .contentType(MediaType.APPLICATION_JSON)
                        .accept(MediaType.APPLICATION_JSON)).andExpect(status().isCreated());

using asJsonString:

public static String asJsonString(final Object obj) {
        try {
            ObjectMapper objectMapper = new ObjectMapper();
            objectMapper.registerModule(new JavaTimeModule())
                    .disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
            return objectMapper.writeValueAsString(obj);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

then I have this exception that I can't understand why it's been raised:

java.time.format.DateTimeParseException: Text '2019-08-16T07:14:06.303Z' could not be parsed at index 20

    

Thanks a lot for your help!!!

0 Answers
Related