用于整数...参数的 Mockito 匹配器

w33*_*z33 3 java unit-testing mocking mockito

此方法签名中第二个参数的正确 Mockito 匹配器是什么:

List<Something> findSomething(Object o, Integer... ids);
Run Code Online (Sandbox Code Playgroud)

我尝试了以下匹配器:

when(findSomething(any(), anyInt())).thenReturn(listOfSomething);
when(findSomething(any(), any())).thenReturn(listOfSomething);
Run Code Online (Sandbox Code Playgroud)

但 Mockito 没有为我创建代理,返回的List是空的。

Nic*_*tto 5

anyVararg()像这样使用:

Application application = mock(Application.class);
List<Application> aps = Collections.singletonList(new Application());

when(application.findSomething(any(), anyVararg())).thenReturn(aps);

System.out.println(application.findSomething("foo").size());
System.out.println(application.findSomething("bar", 17).size());
System.out.println(application.findSomething(new Object(), 17, 18, 19, 20).size());
Run Code Online (Sandbox Code Playgroud)

输出:

1
1
1
Run Code Online (Sandbox Code Playgroud)