TIL - Mockito 테스트 코드 argument matchers 사용 오류

su·2023년 9월 4일
0

TIL

목록 보기
69/93
post-thumbnail

테스트 코드 작성 중 오류

Mockito - when() 조건

when(mongoTemplate.findOne(any(Query.class), PrivateChatRoom.class)).thenReturn(null);

mongoTemplate에서 조건에 맞춰서 데이터를 찾아와야 하는 부분이 있었는데,
findOne 메소드를 사용해야 해서 쿼리문 클래스와, 찾아와야 하는 클래스 타입을 지정해주고
코드를 돌려보니 오류가 났다.

Invalid use of argument matchers!
2 matchers expected, 1 recorded:
-> at ~

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.

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

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"));
...

argument matchers를 잘못 사용했다는 건가 ..?
잘못된 방식이라고 알려준 부분을 보면 someMethod(any(), "raw String"); 라고 한다.
나는 .. 그렇게는 사용하지 않았는데 왜 raw String이라고 하는거지 ?
내가 matchers를 잘못 사용했나보다 ..! 싶어서 알려준 방식대로 eq() 메소드를 사용하기로 했다.

따라서 아래와 같은 코드로 변경했다.

when(mongoTemplate.findOne(any(Query.class), eq(PrivateChatRoom.class))).thenReturn(null);

그랬더니 테스트가 오류 없이 진행되었고, 원하는 결과를 반환했다 !

profile
(❁´◡`❁)

0개의 댓글