Test코드 BDDMockito

현보·2023년 1월 18일

TDD와 BDD에서 사용되는 given/when/then 행동과 실습
https://go-coding.tistory.com/102

Test
    @DisplayName("팔로잉 알림 테스트")
    @WithMockUser(username = "user2")
    void alarm_following_list() throws Exception {
        Page<AlarmResponse> responsePage = new PageImpl<>(List.of(response));
        given(alarmService.findByFollowingReview(any(Pageable.class), eq("user2"))).willReturn(responsePage);

        mockMvc.perform(get("/api/v1/alarms/reviews")
                        .with(csrf()))
                .andExpect(status().isOk())
                .andExpect(jsonPath("$..['id']").exists())
                .andExpect(jsonPath("$..['alarmType']").exists())
                .andExpect(jsonPath("$..['targetUser']").exists())
                .andExpect(jsonPath("$..['fromUser']").exists())
                .andExpect(jsonPath("$..['source']").exists())
                .andDo(print());

        verify(alarmService).findByFollowingReview(any(Pageable.class), eq("user2"));

    }

처음에 any(Pageable.class) 대신 pageable을 넣어줬는데 에러가 발생하였다, given에서 받아오는 주소값이랑 verify에서 검증하는 주소값이 다르면 에러가 발생 할 수 있다고 하여 any()로 통일하게 되었다.

any() - 메서드의 파라미터를 가져온다

  • any()를 사용하면 뒤에 값도 any() 거나 eq() 안에 매개변수를 넣어줘야 에러가 발생하지 않는다.

0개의 댓글