下面的语句有什么作用:
// The question is about the arguments being passed in the function.
SomeReturnOutput = CallSomeFunction(with(any(Long.class)), with(any(List.class)));
我尝试寻找它,但找不到满意的答案。
with(any(Long.class))
和 with(any(List.class))
返回什么?
最佳答案
我所知道的 any()
的一个“用法” - 这是像 Mockito 这样的模拟库所使用的东西。请参阅here例如。
准确地说:当您有一个模拟对象并指定“预期”调用时,您将执行以下操作:
when(someMock.someMethod(any())).thenReturn(whatever);
这基本上告诉框架:传递的任何对象都应该“匹配”。相比之下:
when(someMock.someMethod(someSpecificValue)).thenReturn(whatever);
这意味着:仅当使用该特定值调用 someMethod() 时,才应返回任何内容。
any(SomeClass.class)
基本上是一个“遗留”版本 - 明确说明预期的类。请参阅here例如,获取更多文档。
不过不知道with()
。
关于java - with() 和any() 方法做什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45977595/