Issue
I have a service that I am mocking using Mockito. Is there any way that I can verify the parameters passed to the service call?
For example:
I have a service named employeeAnalyzer.calculateAnnualAppraisalForEmployee(Employee emp)
.
So from unit test I would like to do something like
verifyThat ("John", emp.getName());
verifyThat ("Doe", emp.getLastName);
Basically I want to spy on the parameter that was sent to the service which I inject using
@InjectMocks
EmployeeAnalyzer employeeAnalyzer;
Thanks in advance.
Solution
Definitely! Mockito is awesome. You can use an ArgumentCaptor to capture the Employee
parameter and then do some assertions.
(example taken from the previous link)
ArgumentCaptor<Person> argument = ArgumentCaptor.forClass(Person.class);
verify(mock).doSomething(argument.capture());
assertEquals("John", argument.getValue().getName());
Or the new funky way using the @Captor annotation
@Captor ArgumentCaptor<AsyncCallback<Foo>> captor;
@Before
public void init(){
MockitoAnnotations.initMocks(this);
}
@Test public void shouldDoSomethingUseful() {
//...
verify(mock).doStuff(captor.capture());
assertEquals("foo", captor.getValue());
}
Answered By - Augusto