수진개발서

sns만들기 프로젝트 3일차 마이피드 controller 테스트 코드를 작성하였다.

요구사항은 다음과 같다.

1. 요구사항

- 마이피드 조회 성공
- 마이피드 조회 실패(1) - 로그인 하지 않은 경우

2. 코드

2.1. PostControllerTest

이전의 좋아요 테스트와 다른 사항은 없으며 mock 라이브러리를 사용하여 테스트를 진행하였다.

<code />
@Test @DisplayName("마이피드 조회 성공") @WithMockUser //인증된 상태 void myPeed_success() throws Exception { String url = "/api/v1/posts/my"; //데이터 만들기 PostGetResponse postGetResponse = PostGetResponse.builder() .id(1L) .title("제목") .body("내용") .userName("sujin") .build(); List<PostGetResponse> postGetResponseList = Arrays.asList(postGetResponse); //service 정의 given(postService.getMyPost(any(), any())).willReturn(postGetResponseList); //해당 url로 get요청 mockMvc.perform(get(url) .with(csrf())) .andExpect(status().isOk()) //해당 내용이 있는지 테스트 .andExpect(jsonPath("$.result.content[0].id").value(1L)) .andExpect(jsonPath("$.result.content[0].title").value("제목")) .andExpect(jsonPath("$.result.content[0].body").value("내용")) .andExpect(jsonPath("$.result.content[0].userName").value("sujin")) .andDo(print()); } @Test @DisplayName("마이피드 조회 실패(1) - 로그인 하지 않은 경우") @WithAnonymousUser void myPeed_fail_non_login() throws Exception { String url = "/api/v1/posts/my"; //service 정의 given(postService.getMyPost(any(), any())).willThrow(new AppException(ErrorCode.INVALID_PERMISSION,ErrorCode.INVALID_PERMISSION.getMessage())); //해당 url로 get요청 mockMvc.perform(get(url) .with(csrf())) .andExpect(status().isUnauthorized()) //해당 내용이 있는지 테스트 .andDo(print()); }
profile

수진개발서

@sujin_park0607

포스팅이 좋았다면 "좋아요❤️" 또는 "구독👍🏻" 해주세요!