본문 바로가기

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

[24] mutsa-SNS-2 6일차 - (1) 알람 기능 test code

알람 기능의 controller의 test code를 추가하였다.

해당 test는 아래 두가지로 진행하였다.

  • 알람 목록 조회 성공
  • 알람 목록 조회 실패 - 로그인하지 않은 경우

 

AlarmControllerTest

@WebMvcTest(AlarmController.class)
class AlarmControllerTest {

    @Autowired
    MockMvc mockMvc;

    @MockBean
    AlarmService alarmService;

    @Test
    @DisplayName("알람 목록 조회 성공")
    @WithMockUser
    void alarm_get_success() throws Exception {

        when(alarmService.getAlarm(any(), any()))
                .thenReturn(Page.empty());


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

    @Test
    @DisplayName("알람 목록 조회 실패 - 로그인하지 않은 경우")
    @WithAnonymousUser
    void alarm_get_fail() throws Exception {

        when(alarmService.getAlarm(any(), any()))
                .thenReturn(Page.empty());


        mockMvc.perform(get("/api/v1/alarms")
                        .with(csrf())
                        .contentType(MediaType.APPLICATION_JSON))
                .andExpect(status().isUnauthorized())
                .andDo(print());
    }


}

역시 기존의 Controller test와 거의 동일하다.