Mockito可以在不单独指定每个参数匹配器的情况下模拟一个方法吗



假设我有一些带有5个参数的方法,比如

public int foo(int arg1, int arg2, int arg3, int arg4, int arg5) {
//do something
}

当我嘲笑它时,我最终会做一些类似的事情

Mockito.when(myClass.foo(anyInt(), anyInt(), anyInt(), anyInt(), anyInt())).thenReturn(expectedResult);
//do stuff
Mockito.verify(myClass).foo(expectedArg1, expectedArg2, expectedArg3, expectedArg4, expectedArg5);

有没有一种方法可以将所有的anyInt()匹配器组合成对眼睛更容易的东西?类似Collections.nCopies(5, anyInt());

重构foo方法的参数总是会更好。使用包含所有参数的包装器——这不仅会让测试部分的眼睛更容易看到,而且会让你的类结构更清晰。关于您的请求,这是不可能的,因为您使用的是myClass.foo(...)方法,您必须逐个指定参数。

相关内容

最新更新