SNS 프로젝트 중 NestedServletException

개발연습생log·2023년 1월 4일
0

Problem

목록 보기
1/4
post-thumbnail

❗이슈

이슈가 일어난 배경

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.mockito.exceptions.misusing.InvalidUseOfMatchersException: 
Invalid use of argument matchers!
3 matchers expected, 1 recorded:
-> at com.example.project_like_lion_sns.controller.CommentControllerTest.comment_write_SUCCESS(CommentControllerTest.java:61)

This exception may occur if matchers are combined with raw values:
    //incorrect:
    someMethod(any(), "raw String");
When using matchers, all arguments have to be provided by matchers.
For example:
    //correct:
    someMethod(any(), eq("String by matcher"));

For more info see javadoc for Matchers class.
  • 미니 sns 프로젝트에서 테스트를 진행하다 발생한 예외이다.

원인

    @Test
    @DisplayName("댓글 작성 성공")
    @WithMockUser
    void comment_write_SUCCESS() throws Exception {

        when(commentService.write(any(), any(), any()))
                .thenReturn(commentResponse);

        mockMvc.perform(post("/api/v1/posts/1/comments")
                        .with(csrf())
                        .contentType(MediaType.APPLICATION_JSON)
						 // 이 부분에서 예외가 발생                       
 						.content(objectMapper.writeValueAsBytes(new CommentRequest(any()))))
                .andDo(print())
                .andExpect(status().isOk());
    }
  • mockMVC perform 요청 메소드에 content 매개변수를 any()로 넘겨서 생긴 문제

해결

  • any()가 아닌 ConmmentRequest 매개변수인 String값을 넣어주면서 해결했다.
  • 아래와 같이 변경
  • any() -> `"테스트입니다"
profile
주니어 개발자를 향해서..

0개의 댓글