JacksonTester ignores @JsonProperty in @JsonTest

Viewed 38

I have a POJO in which I annotated the properties with @JsonProperty.

@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class ResponseDto {

    @JsonProperty("error-id")
    private MyError errorId;

    @JsonProperty("my-message")
    private String myMessage;

    @JsonProperty("my-status")
    private int myStatus;
}

Now I want to test serialization of this POJO using JacksonTester:

@JsonTest
public class ResponseDtoJsonTest {
    @Autowired
    private JacksonTester<ResponseDto> responseDtoTester;
    @Autowired
    private JacksonTester<MyError> myErrorTester;

    private static final MyError MY_ERROR_ID = MyError.MY_CONFIG_ERROR;
    private static final int STATUS = HttpStatus.BAD_REQUEST_400;
    private static final String MESSAGE = "blablabla";

    private static ResponseDto responseDto;

    @BeforeAll
    static void init() {
        responseDto = ResponseDto.builder().errorId(MY_ERRORER_ID).status(STATUS).message(MESSAGE).build();
    }

    @Test
    void errorIdSerializes() throws IOException {
        JsonContent<MyError> myErrorResult = myErrorTester.write(responseDto.getErrorId());

        assertThat(myErrorResult).hasJsonPathNumberValue("@.error-id");
        assertThat(myErrorResult).hasJsonPathStringValue("@.error-text");
        assertThat(myErrorResult).hasJsonPathStringValue("@.result");
    }
}

MyError is an ENUM with this structure...

public enum MyError implements BaseError {

    UNEXPECTED_ERROR(10000, "Unexpected error", Result.FAILED),
    ...
    ;

    @JsonProperty("error-id")
    public final int errorId;

    @JsonProperty("error-text")
    public final String errorText;

    @JsonProperty("result")
    public final Result result;
}

The problem is that JacksonTester does not recognize the @JsonProperty annotations at the properties in MyError class which means, that

assertThat(myErrorResult).hasJsonPathNumberValue("@.errorId");
assertThat(myErrorResult).hasJsonPathStringValue("@.errorText");
assertThat(myErrorResult).hasJsonPathStringValue("@.result");

works but

assertThat(myErrorResult).hasJsonPathNumberValue("@.error-id");
assertThat(myErrorResult).hasJsonPathStringValue("@.error-text");
assertThat(myErrorResult).hasJsonPathStringValue("@.result");

does not.

0 Answers
Related