Ich habe ein Befehlszeilentool, das eine DNS-Überprüfung durchführt. Wenn die DNS-Prüfung erfolgreich ist, fährt der Befehl mit weiteren Aufgaben fort. Ich versuche mit Mockito Unit-Tests zu schreiben. Hier ist mein Code: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
}
}
ich InetAddressFactory bin mit einer statischen Umsetzung der InetAddress
Klasse zu verspotten. Hier ist der Code für die Fabrik:
public class InetAddressFactory {
public InetAddress getByName(String host) throws UnknownHostException {
return InetAddress.getByName(host);
}
}
Hier ist meine Einheit Testfall:
@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();
}
}
Ausnahme auf Lauf testPostDnsCheck()
Test:
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"));
Jede Eingabe, wie dieses Problem zu lösen?
Wie dumm von mir. Ich habe weiter analysiert, warum der zweite Parameter mir immer den Fehler gab. Danke fürs klarstellen. Ich bin sehr neu in Mockito, dies ist meine erste Begegnung. – devang
Danke Mann. Die Ausnahmebedingungsnachricht, wenn sehr verwirrend, aber Ihre Erklärung war sehr klar zu verstehen –