조회 api 의 통합 test 를 작성하던중 api 의 json 응답값을 java 객체로 변환하지 못하는 오류가 발생했다.
MockMvc 요청을 보내고 반환값 Json 을 java 객체로 변환하는 테스트 코드
@Test
void no2() throws Exception {
ResultActions result = patchReq(mvc,
mapping + "/v2/leader", jwt1, reqDto);
StudyResDto resDto = toResDto(result, StudyResDto.class);
}
public static <T> T toResDto(ResultActions result, Class<T> data) throws UnsupportedEncodingException, JsonProcessingException {
ObjectMapper mapper = new ObjectMapper();
MvcResult mvcResult = result.andReturn();
return mapper.readValue(
mvcResult.getResponse().getContentAsString(), data);
}
com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Java 8 date/time type `java.time.LocalDateTime` not supported by default: add Module "com.fasterxml.jackson.datatype:jackson-datatype-jsr310" to enable handling
at [Source: (String)"{"id":1,"createDate":[2023,10,7,11,52,39,940757000],"modifyDate":[2023,10,7,11,52,40,231822000],"name":"study1","about":"","leader":2,"capacity":10,"studyMember":2,"xp":0.0,"bronze":0,"silver":0,"gold":0,"diamond":0,"ruby":0,"platinum":0,"solvedCount":0,"ranking":null}"; line: 1, column: 22] (through reference chain: com.baeker.study.study.in.resDto.StudyResDto["createDate"])
LocalDateTime
이 포함되어있었고 이 값을 변환하는데 실패가 원인이다.@Data
@NoArgsConstructor
public class StudyResDto {
private Long id;
private LocalDateTime createDate; // 변환 실패
private LocalDateTime modifyDate;
public static <T> T toResDto(ResultActions result, Class<T> data) throws UnsupportedEncodingException, JsonProcessingException {
ObjectMapper mapper = new ObjectMapper();
MvcResult mvcResult = result.andReturn();
return mapper
.registerModule(new JavaTimeModule()) // 모듈 추가
.readValue(mvcResult.getResponse().getContentAsString(), data);
}