DNS 검사를 수행하는 명령 줄 도구가 있습니다. DNS 검사가 성공하면 명령은 추가 작업을 진행합니다. Mockito를 사용하여 이것에 대한 단위 테스트를 작성하려고합니다. 나는 InetAddress
클래스의 정적 구현을 조롱 InetAddressFactory을 사용하고Mockito : InvalidUseOfMatchersException
public class Command() {
// ....
void runCommand() {
// ..
dnsCheck(hostname, new InetAddressFactory());
// ..
// do other stuff after dnsCheck
}
void dnsCheck(String hostname, InetAddressFactory factory) {
// calls to verify hostname
}
}
: 여기에 내 코드입니다. 여기에 공장에 대한 코드는 다음과 같습니다
public class InetAddressFactory {
public InetAddress getByName(String host) throws UnknownHostException {
return InetAddress.getByName(host);
}
}
여기 내 단위 테스트 케이스이다 :
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"));
이 문제를 해결하는 방법에 대한 입력 : testPostDnsCheck()
테스트를 실행에
@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();
}
}
예외?
내게 어리석은. 나는 왜 제 2 인자가 나에게 오류를주는 지 계속 분석했다. 명확히 해 주셔서 감사합니다. 나는 Mockito에게 아주 새로운데, 이것은 나의 첫 만남이다. – devang
고마워요. 예외 메시지는 매우 혼란 스럽지만, 당신의 설명이 이해하기 매우 명확했습니다. –