我想测试myClass,我的意思是要测试公共功能myfunction。此功能呼吁MyService的物种途径。我想检查它是否通过创建的有效参数str1和str2在这堂课。我当时正在考虑捕获这些捕获,但是我不确定是否可以捕获2个参数或如何做。如果可能的话,我不想更改可见度
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());
}
}
您只需要两个参数绑架
@Mock
private Service service;
@Captor
private ArgumentCaptor<String> strOneCaptor;
@Captor
private ArgumentCaptor<String> strTwoCaptor;
在测试中:
Mockito.verify(service).someMethod(strOneCaptor.capture(), strTwoCaptor.capture());
assertEquals(strOneCaptor.getValue(), expectedStrOne);
assertEquals(strTwoCaptor.getValue(), expectedStrTwo);