본문 바로가기

프로젝트/멋사 개인 프로젝트 (mutsa-SNS)

[22] mutsa-SNS-2 4일차 - 마이 피드 기능 test code

마이 피드 기능 test code를 추가하였다.

아래의 case를 테스트 해보았다.

 

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

 

PostControllerTest

@WebMvcTest(PostController.class)
class PostControllerTest {
    @Autowired
    MockMvc mockMvc;

    @MockBean
    PostService postService;

    @Autowired
    ObjectMapper objectMapper;
    
    ...
    
    
    @Test
    @DisplayName("마이피드 조회 성공")
    @WithMockUser
    void myFeed_get_success() throws Exception {

        when(postService.getMyFeed(any(), any()))
                .thenReturn(Page.empty());


        mockMvc.perform(get("/api/v1/posts/my")
                .with(csrf())
                .contentType(MediaType.APPLICATION_JSON))
                .andExpect(status().isOk())
                .andExpect(jsonPath("$.resultCode").value("SUCCESS"))
                .andDo(print());
    }


    @Test
    @DisplayName("마이피드 조회 실패 - 로그인 하지 않은 경우")
    @WithAnonymousUser
    void myFeed_get_fail1() throws Exception {

        mockMvc.perform(delete("/api/v1/posts/my")
                        .with(csrf())
                        .contentType(MediaType.APPLICATION_JSON))
                .andExpect(status().isUnauthorized())
                .andDo(print());

    }


}

기존의 테스트코드 방식과 거의 동일하다.