I am writing a test case using Mockito, and have to write expectation for a method (signature shown below)
public Object process(Employee e);
In my test class, I have to simulate my expectations as follows:
when(someClass.process("any Employee with id between 1 and 100.").thenReturn(object1);
when(someClass.process("any Employee with id between 101 and 200.").thenReturn(object2);
How can I set expectations conditionally.
You can do it using Mockito Answer
final ArgumentCaptor<Employee> employeeCaptor = ArgumentCaptor.forClass(Employee.class);
Mockito.doAnswer(invocation -> {
Employee employee = employeeCaptor.getValue();
if(employee.getId() > 1 && employee.getId() < 100)
return object1;
else if(employee.getId() > 101 && employee.getId() < 200)
return object2;
else someOtherObject;
}).when(someClass).process(employeeCaptor.capture());