❗이슈
이슈가 일어난 배경
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()
-> `"테스트입니다"