Reputation: 1099
I want to test MyClass, and I mean by that to test the public function myFunction. This function calls to someMethod from MyService. I want to check that it passes valid arguments str1 and str2 that were create in this class. I was thinking about capture those, but I'm not sure if it is possible to capture 2 arguments, or how to do it. I don't want to change the visibility if possible
class MyService
{
public void someMethod(String str1, String str2);
}
class MyClass
{
private MyService myService;
private String createStrOne(){...};
private String createStrTwo(){...};
....
public void myFunction()
{
myService = new MyService();
myService.someMethod(createStrOne(),createStrTwo());
}
}
Upvotes: 4
Views: 7418
Reputation: 26492
You would simply need two argument captors
@Mock
private Service service;
@Captor
private ArgumentCaptor<String> strOneCaptor;
@Captor
private ArgumentCaptor<String> strTwoCaptor;
in the test:
Mockito.verify(service).someMethod(strOneCaptor.capture(), strTwoCaptor.capture());
assertEquals(strOneCaptor.getValue(), expectedStrOne);
assertEquals(strTwoCaptor.getValue(), expectedStrTwo);
Upvotes: 9