ResultActions resultActions = mvc
.perform(
get("/api/v1/posts")
)
.andDo(print());
resultActions
.andExpect(handler().handlerType(ApiPostController.class)) //handlerType : 어떤 컨트롤러가 처리했는지 검증
.andExpect(handler().methodName("getItems")) //handlerType : 어떤 컨트롤러가 처리했는지 검증, methodName : 어떤 메서드가 처리했는지 검증
.andExpect(status().isOk()) //isOk : HTTP 상태 코드 200을 기대
;
List<Post> posts = postRepository.findAll();
for(int i=1; i<posts.size(); i++){
resultActions
.andExpect(jsonPath("$[%d].id".formatted(i)).value(posts.get(i).getId())) //jsonPath : JSON 응답에서 특정 필드의 값을 검증하는 데 사용
.andExpect(jsonPath("$[%d].createDate".formatted(i)).value(matchesPattern(posts.get(i).getCreateDate().toString()))) //exists() : 해당 필드가 존재하는지 검증
.andExpect(jsonPath("$[%d].modifyDate".formatted(i)).value(posts.get(i).getModifyDate().toString())) //value() : 해당 필드의 값이 기대한 값과 일치하는지 검증
.andExpect(jsonPath("$[%d].title".formatted(i)).value(posts.get(i).getTitle()))
.andExpect(jsonPath("$[%d].body".formatted(i)).value(posts.get(i).getContent()));
}
resultActions
.andExpect(jsonPath("$.length()").value(3))
.andExpect(jsonPath("$[*].id", containsInRelativeOrder(1,3))) //containsInRelativeOrder : 특정 값들이 순서대로 포함되어 있는지 검증
;