I'm trying to use the when....then functions of mockito. And I'm wondering if it's possible to put in a list for the then portion but for it to iterate through the list and return a different object. So if I had a list with 3 objects: A, B, C
I want to be able to iterate through the list and return the next object in the list. So if the method inside of when gets called 3 times, the first time it would return A, the second time B, and the third time C. Here's my current code:
public class fooTest {
private Foo foo;
private List<Car> cars;
private TestHelper helper;
@Mock
private DomainService service;
private Car car;
@Before
public void setUp() throws Exception {
MockitoAnnotations.initMocks(this);
foo = new Foo();
helper = new TestHelper();
cars = new ArrayList<Car>();
Mockito.when(service.findObjectByID(any())).thenAnswer( AdditionalAnswers.returnsElementsOf(cars) );
}
@Test
public void testVin() {
cars = helper.generateCars(5, /*random number*/);
Fleet result = foo.setFleet(Arrays.asList( helper.makeCars(3) );
assertEquals(/*list of numbers*/, result.getCars().getVINs());
}
}
public class TestHelper {
public Fleet makeCars(int numCars) {
Fleet fleet = new Fleet();
Car car;
for(int i=0; i<numCars; i++) {
car = new Car(i, /*some vin number*/);
fleet.add(car);
}
return fleet;
}
public List<Car> generateCars(int numCars, int vinNum) {
//implementation
}
}
public class Foo {
private DomainService domainService;
// Constructor to initialize
public Fleet setFleet(List<Car> cars) {
....
Optional<Vehicle> vehicle = domainService.findObjectByID(/*Some number*/);
....
}
}
public class DomainService {
Optional<Vehicle> findObjectByID(long id) {
//implementation
}
}
Vehicle is the class Car extends. My problem is every time findObjectByID gets called the vehicle object is null. That makes sense because the cars object is initialized as an empty list and the mockito whenfunction uses that as the return list instead of the populated one made in the test function. I also get this runtime error:
java.lang.ClassCastException: com.jer.test.model.Car cannot be cast to com.google.common.base.Optional
But I've changed my code and tried it this way as well:
@Before
public void setUp() throws Exception {
}
@Test
public void testVin() {
cars = helper.generateCars(5, /*random number*/);
Mockito.when(service.findObjectByID(any())).thenAnswer( AdditionalAnswers.returnsElementsOf(cars) );
Fleet result = foo.setFleet(Arrays.asList( helper.makeCars(3) );
assertEquals(/*list of numbers*/, result.getCars().getVINs());
}
But the vehicle object is still null. I have no idea why. Any place I've gone wrong?
Re-attempt 1: I've tried the following with no luck:
@Before
public void setUp() throws Exception {
MockitoAnnotations.initMocks(this);
foo = new Foo();
helper = new TestHelper();
}
@Test
public void testVin() {
cars = helper.generateCars(5, /*random number*/);
Mockito.when(service.findObjectByID(any())).then( new ReturnsElementsOf(bFIs) );
Fleet result = foo.setFleet(Arrays.asList( helper.makeCars(3) );
assertEquals(/*list of numbers*/, result.getCars().getVINs());
}
Re-attempt 2: I've tried the following with some luck:
@Before
public void setUp() throws Exception {
MockitoAnnotations.initMocks(this);
foo = new Foo();
helper = new TestHelper();
cars = new ArrayList<Car>();
cars.add(new Car(1, 2));
Mockito.when(service.findObjectByID(any())).thenAnswer( AdditionalAnswers.returnsElementsOf(cars) );
}
@Test
public void testVin() {
cars = helper.generateCars(5, /*random number*/);
Fleet result = foo.setFleet(Arrays.asList( helper.makeCars(3) );
assertEquals(/*list of numbers*/, result.getCars().getVINs());
}
And this seems to work. The vehicle object returned is the cars object made in the setUp class but this isn't really what I want because I want the return to be from a list or different car objects.
Re-attempt 3: I've tried Florian Schaetz's suggestion but I'm still getting null returned when findObjectByID is called:
@Before
public void setUp() throws Exception {
MockitoAnnotations.initMocks(this);
foo = new Foo();
helper = new TestHelper();
}
@Test
public void testVin() {
cars = helper.generateCars(5, /*random number*/);
for(Car car : cars) {
Mockito.when(service.findObjectByID(any())).thenReturn( Optional.of(car) );
}
Fleet result = foo.setFleet(Arrays.asList( helper.makeCars(3) );
assertEquals(/*list of numbers*/, result.getCars().getVINs());
}