When I mock an object with Mockito.mock(), I expect the object to have stubbed out methods that return null, 0, false, et al, without having any of the code of the real object I'm mocking. I thought this was the default behavior in Java, but Android seems to involve the real objects as part of the mocks. How do I avoid this?
public class MockTest extends InstrumentationTestCase {
public void testMock() {
Engine engine = mock(Engine.class);
Car car = new Car(engine);
car.start(); // Null pointer error, because Engine.starter is null.
verify(engine, Mockito.times(1)).engageStarter();
}
public static class Car {
private final Engine engine;
public Car(Engine engine) {
this.engine = engine;
}
public void start() {
engine.engageStarter();
}
}
public static class Engine {
private final Starter starter;
public Engine(Starter starter) {
this.starter = starter;
}
void engageStarter() {
starter.spin();
}
}
public static class Starter {
public void spin() {
System.out.println("Start or explode");
}
}
}