모키 토 : InvalidUseOfMatchersException
DNS 확인을 수행하는 명령 줄 도구가 있습니다. DNS 확인에 성공하면 명령은 추가 작업을 진행합니다. Mockito를 사용하여 단위 테스트를 작성하려고합니다. 내 코드는 다음과 같습니다.
public class Command() {
// ....
void runCommand() {
// ..
dnsCheck(hostname, new InetAddressFactory());
// ..
// do other stuff after dnsCheck
}
void dnsCheck(String hostname, InetAddressFactory factory) {
// calls to verify hostname
}
}
InetAddress
클래스 의 정적 구현을 조롱하기 위해 InetAddressFactory를 사용하고 있습니다. 공장 코드는 다음과 같습니다.
public class InetAddressFactory {
public InetAddress getByName(String host) throws UnknownHostException {
return InetAddress.getByName(host);
}
}
내 단위 테스트 사례는 다음과 같습니다.
@RunWith(MockitoJUnitRunner.class)
public class CmdTest {
// many functional tests for dnsCheck
// here's the piece of code that is failing
// in this test I want to test the rest of the code (i.e. after dnsCheck)
@Test
void testPostDnsCheck() {
final Cmd cmd = spy(new Cmd());
// this line does not work, and it throws the exception below:
// tried using (InetAddressFactory) anyObject()
doNothing().when(cmd).dnsCheck(HOST, any(InetAddressFactory.class));
cmd.runCommand();
}
}
testPostDnsCheck()
테스트 실행시 예외 :
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(anyObject(), "raw String");
When using matchers, all arguments have to be provided by matchers.
For example:
//correct:
someMethod(anyObject(), eq("String by matcher"));
이 문제를 해결하는 방법에 대한 의견이 있으십니까?
오류 메시지는 솔루션을 매우 명확하게 설명합니다. 라인
doNothing().when(cmd).dnsCheck(HOST, any(InetAddressFactory.class))
모든 원시 값 또는 모든 매처를 사용해야하는 경우 하나의 원시 값과 하나의 매처를 사용합니다. 올바른 버전이 읽을 수 있습니다
doNothing().when(cmd).dnsCheck(eq(HOST), any(InetAddressFactory.class))
I had the same problem for a long time now, I often needed to mix Matchers and values and I never managed to do that with Mockito.... until recently ! I put the solution here hoping it will help someone even if this post is quite old.
It is clearly not possible to use Matchers AND values together in Mockito, but what if there was a Matcher accepting to compare a variable ? That would solve the problem... and in fact there is : eq
when(recommendedAccessor.searchRecommendedHolidaysProduct(eq(metas), any(List.class), any(HotelsBoardBasisType.class), any(Config.class)))
.thenReturn(recommendedResults);
In this example 'metas' is an existing list of values
It might help some one in the future: Mockito doesn't support mocking of 'final' methods (right now). It gave me the same InvalidUseOfMatchersException
.
The solution for me was to put the part of the method that didn't have to be 'final' in a separate, accessible and overridable method.
Review the Mockito API for your use case.
Inspite of using all the matchers, I was getting the same issue:
"org.mockito.exceptions.misusing.InvalidUseOfMatchersException:
Invalid use of argument matchers!
1 matchers expected, 3 recorded:"
It took me little while to figure this out that the method I was trying to mock was a static method of a class(say Xyz.class) which contains only static method and I forgot to write following line:
PowerMockito.mockStatic(Xyz.class);
May be it will help others as it may also be the cause of the issue.
For my case, the exception was raised because I tried to mock a package-access
method. When I changed the method access level from package
to protected
the exception went away. E.g. inside below Java class,
public class Foo {
String getName(String id) {
return mMap.get(id);
}
}
the method String getName(String id)
has to be AT LEAST protected
level so that the mocking mechanism (sub-classing) can work.
참고URL : https://stackoverflow.com/questions/14845690/mockito-invaliduseofmatchersexception
'Programing' 카테고리의 다른 글
MVC에서 기본 경로 (영역으로)를 설정하는 방법 (0) | 2020.07.15 |
---|---|
문서 디렉토리 (NSDocumentDirectory)는 무엇입니까? (0) | 2020.07.15 |
불투명도 : 0은 가시성 : 숨김과 효과가 동일합니다. (0) | 2020.07.15 |
파일의 확장자를 찾는 방법? (0) | 2020.07.15 |
Python 3.0,3.1,3.2의 "ValueError : 길이가 0 인 필드 이름"오류 (0) | 2020.07.15 |