Reputation: 21
I have a method
class Garage{
public Noise myMethod(){
Car mycar = getCarService().getCar("ford");
Noise brum = mycar.drive();
return brum;
}
...
}
I want to mock both the service and the car, so I have created a mock like this
MyCarService carMock = createMock(Car.class)
MyCarService mockServce = createMock(MyCarService.class)
expect(mockService.getCarService().andReturn(carMock));
expect(carMock.drive().andReturn("brummmm"));
replayAll();
Garage garage = new Garage();
garage.setCarService(mockService);
Noise n = g.myMethod();
However when I run the code mycar is always returned from the mockservice as null. Can you do this type of thing with easyMock?
Upvotes: 2
Views: 1357
Reputation: 1313
You should not include this line: garage.setCarService(mockService);
.
All you need is your expectation of mockService.getCarService() being called, which you have done.
So, when you run your test by invoking g.myNewMethod, when myNewMethod hits the getCarService() method, it will return your mockService.
You are however missing an expectation for the getCar method being called. You need:
expect(mockServce.getCar("ford")).andReturn(carMock);
Upvotes: 1