This is a slightly simplified version of the result that I'm trying to achieve but I think it illustrates the problem.
Imagine I have the following two classes, where one is a descendant of the other:
public class Vehicle {
protected String name;
{
name = "Vehicle";
}
public String getName() {
return name;
}
}
public class Car extends Vehicle {
{
name = "Car";
}
}
I also have this test code:
public class VehiclesTest {
@Test
public void checkVehicles(@Mocked final Vehicle vehicleMock,
@Mocked final Car carMock) {
new Expectations() {
{
vehicleMock.getName(); result = "mocked vehicle";
carMock.getName(); result = "mocked car";
}
};
Vehicle aVehicle = new Vehicle();
System.out.println("Starting a " + aVehicle.getName());
Vehicle aCar = new Car();
System.out.println("Starting a " + aCar.getName());
}
}
What I want to see on the console output is this:
Starting a mocked vehicle Starting a mocked car
However, the actual output looks like this:
Starting a mocked car Starting a mocked car
I'm new to JMockit so I think I know why this is happening (something due to the fact that JMockit will mock all ancestor classes of a mock all the way up the class hierarchy, excluding java.lang.Object).
How can I set up my expectations so that I get the result that I want? Is it possible to set expectations on multiple mocks of different classes in the same hierarchy (i.e. where one is a descendant of the other)?