我是测试新手。我试过这个但有一个异常(exception)。
@Mock
private Context context;
...
when(service.getResult(any(), context)).thenReturn(new ArrayList<>());
异常(exception):
org.mockito.exceptions.misusing.InvalidUseOfMatchersException:
Invalid use of argument matchers!
2 matchers expected, 1 recorded:
最佳答案
Mockito.any()
是一个 Mockito 参数匹配器。当您在模拟记录中定义的参数之一中指定它时,您必须对所有参数使用参数匹配器。
Mockito.eq(T)
是将任何参数转换为参数匹配器的方法。
所以这应该没问题:
@Mock
private Context context;
@Mock
private Service service;
...
when(service.getResult(any(), eq(context)).thenReturn(new ArrayList<>());
关于testing - 无效的使用匹配器异常 : Invalid use of argument matchers,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50073912/