Mockito - when ... thenReturn 多次通过传递期望值列表
Mockito - when ... thenReturn multiple times by passing a list of expected values
为什么 Mockito 不支持 thenReturn
方法中的集合?
我要
// mockObject.someMethod() returns an instance of "Something".
// Want to achieve that call mockObject.someMethod the first time returns Something_1, call mockObject.someMethod the second time returns Something_2, call mockObject.someMethod the third time returns Something_3, ...
List<Something> expectedValues = ...;
when(mockObject.someMethod()).thenReturn(expectedValues);
因为 expectedValues
的计数是任意的。
方法 thenReturn
支持可变参数但不支持集合:
Mockito.when(mockObject.someMethod()).thenReturn(something1, something2, ...);
OR
Mockito.when(mockObject.someMethod()).thenReturn(something, arrayOfSomething);
另一种方法是链接 thenReturn
调用:
Mockito.when(mockObject.someMethod()).thenReturn(something1).thenReturn(something2);
return something1
第一次调用 mockObject.someMethod()
和 something2
第二次调用等等
为什么 Mockito 不支持 thenReturn
方法中的集合?
我要
// mockObject.someMethod() returns an instance of "Something".
// Want to achieve that call mockObject.someMethod the first time returns Something_1, call mockObject.someMethod the second time returns Something_2, call mockObject.someMethod the third time returns Something_3, ...
List<Something> expectedValues = ...;
when(mockObject.someMethod()).thenReturn(expectedValues);
因为 expectedValues
的计数是任意的。
方法 thenReturn
支持可变参数但不支持集合:
Mockito.when(mockObject.someMethod()).thenReturn(something1, something2, ...);
OR
Mockito.when(mockObject.someMethod()).thenReturn(something, arrayOfSomething);
另一种方法是链接 thenReturn
调用:
Mockito.when(mockObject.someMethod()).thenReturn(something1).thenReturn(something2);
return something1
第一次调用 mockObject.someMethod()
和 something2
第二次调用等等