Spring/Spring err

[Spring Controller test Error] - Invalid use of argument matchers! - "Mock argument matcher 에러"

민돌v 2022. 5. 26. 16:10
@Test
@DisplayName("Member 정상 수정")
void updateMemberTest() throws Exception {
    String content = objectMapper.writeValueAsString(
        new MemberDto.Request(NAME, EMAIL, PHONE_NUMBER));

    doNothing().when(memberService).update(MEMBER_ID,any(MemberDto.Request.class));

    final ResultActions resultActions = mockmvc.perform(delete("/member")
        .param("memberId",Long.toString(MEMBER_ID))
        .content(content)
        .contentType(MediaType.APPLICATION_JSON)
        .accept(MediaType.APPLICATION_JSON));

    resultActions.andExpect(MockMvcResultMatchers.status().isOk());
}

 

[해결 방안]

서비스 메서드에 인자를 넘겨줄 때 argumentmatcher를 한 인자( ex) - any()) 에서 사용하면 다른 모든 인자도 argumentmatcher 로 넘겨주어야 한다.

 

Long으로 넘겨주는 인자를, argumentmatcher 인자로 넘겨주니까 해결~! ㅠㅠ

doNothing().when(memberService).update(eq(MEMBER_ID),any(MemberDto.Request.class));

 

 

org.mockito.exceptions.misusing.UnfinishedStubbingException: Unfinished stubbing detectedhere:
-> at com.example.springPractice.controller.MemberControllerTest.updateMemberTest(MemberControllerTest.java:87)

E.g. thenReturn() may be missing.
Examples of correct stubbing:
when(mock.isOk()).thenReturn(true);
when(mock.isOk()).thenThrow(exception);
doThrow(exception).when(mock).someVoidMethod();

Hints: 1. missing thenReturn() 2.
you are trying to stub a final method, which is not supported 3. you are stubbing the behaviour of another mock inside before 'thenReturn' instruction is completed

 

 

에러 코드

org.mockito.exceptions.misusing.InvalidUseOfMatchersException:
Invalid use of argument matchers! 2 matchers expected, 1 recorded:

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.

참고

https://velog.io/@aad3365/Mock-argument-matcher%EC%97%90-%EA%B4%80%ED%95%B4%EC%84%9C