Я пытаюсь создать модульный тест для этого метода
public static boolean isOnline(){
final String command = "ping -c 1 google.com";
boolean isConnected = false;
try {
isConnected = Runtime.getRuntime().exec(command).waitFor() == 0;
} catch (InterruptedException | IOException e) {
e.printStackTrace();
}
return isConnected;
}
, но я полностью застрял в этой проблеме. Я пытался сделать это с PowerMockito вот так
@RunWith(PowerMockRunner.class)
@PrepareForTest(Utils.class)
public class TestConnection {
@Mock
private Runtime mockRuntime;
@Mock
private Process process;
@Test
public void test() throws InterruptedException {
String command = "ping -c 1 google.com";
boolean isConnected = false;
PowerMockito.mockStatic(Runtime.class);
try {
process = mockRuntime.exec("ping -c 1 google.com");
} catch (IOException e) {
e.printStackTrace();
}
when(process.waitFor()).thenReturn(0);
if (process.waitFor() == 0){
isConnected = true;
}
Assert.assertThat(isConnected, CoreMatchers.is(Utils.isOnline()));
// do the rest of your test
}
}
я впервые с mockito и powerMokito, и я не знаю, где я сделал Неверно