One of the functions I'm testing is sshing into a machine. I want to mock the ping method, that actually tries to ssh into a machine, since I am not really sshing into a machine.
class I am testing:
public class TestMachine {
public int ping(host){
}
public boolean machineIsGood(host) {
blah blah
int val = ping(host);
blah blah blah
if(val != 0) return false;
return true;
}
}
The test class goes something like this:
public class TestClass {
public void setUp() {
TestMachine tester = spy(new TestMachine());
}
public void testOne() {
when(test.ping(anyString()).thenReturn(-1);
assertFalse(tester.machineIsGood("testHost"));
}
{
The problem is that when I am running them locally, they work just fine, but on our autobuild system it seems like it is actually calling the real ping and getting an Authentication Exception. I'm going to use mock() instead of spy() since I've read it's a bit weird, but I just can't understand what makes a difference in that it is actually calling the method! Just wondering if anyone else has any insight.