我有一个执行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
}
}
Run Code Online (Sandbox Code Playgroud)
我正在使用InetAddressFactory来模拟类的静态实现InetAddress.这是工厂的代码:
public class InetAddressFactory {
public InetAddress getByName(String host) throws UnknownHostException {
return InetAddress.getByName(host);
}
}
Run Code Online (Sandbox Code Playgroud)
这是我的单元测试用例:
@RunWith(MockitoJUnitRunner.class)
public class CmdTest {
// many functional tests for dnsCheck
// here's the piece of code that is failing
// in this test …Run Code Online (Sandbox Code Playgroud)