0

I am writing unit tests for my API controller, on one of the tests - i am facing a problem of deserializing an object when it returns from the endpoint, when debugging it in the controller's function i see that everything goes as expected but the error is being thrown on expectBody(UserDto.class)

It works in production but throws error on test context

        String Id = "123";
        UserTimestampDto userTimestampDto = new             UserTimestampDto(LocalDateTime.now().toString());
        TimestampsDto timestampsDto = new TimestampsDto(userTimestampDto, userTimestampDto);
        UserDto userDto = new UserDto("123",timestampsDto);

        webTestClient
                .get()
                .uri(uriBuilder -> uriBuilder
                        .path(UserController.BASE_URL + "/" + Id)
                        .build())
                .accept(MediaType.APPLICATION_JSON)
                .exchange()
                .expectStatus().isOk()
                .expectBody(UserDto.class)
                .value(userItem -> userItem, equalTo(userDto));

The Error:

org.springframework.core.codec.DecodingException: JSON decoding error: Cannot construct instance of com.project.v1.model.timestamps.UserTimestampDto (although at least one Creator exists): cannot deserialize from Object value (no delegate- or property-based Creator); nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot construct instance of com.project.v1.model.timestamps.UserTimestampDto (although at least one Creator exists): cannot deserialize from Object value (no delegate- or property-based Creator) at [Source: (org.springframework.core.io.buffer.DefaultDataBuffer$DefaultDataBufferInputStream); line: 1, column: 296] (through reference chain: com.project.v1.model.customer.UserDto["internalTimestamps"]->com.project.v1.model.timestamps.TimestampsDto["createdDate"])

  • What type com.project.v1.model.timestamps.TimestampsDto["createdDate"] field has? Deserializer says it cannot construct an object of this type. And could you also post here your DTO classes? – kerbermeister Jan 09 '23 at 15:55
  • Its a string, and its getter returns java's LocalDateTime after parsing the string. – eyal tamsot Jan 10 '23 at 09:40

0 Answers0