Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Capture two arguments with Mockito

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());
}
}
like image 609
Paz Avatar asked Jan 28 '23 17:01

Paz


1 Answers

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);
like image 108
Maciej Kowalski Avatar answered Feb 04 '23 10:02

Maciej Kowalski