Java 莫基托-当。。。然后通过传递期望值列表多次返回
为什么Mockito不支持thenReturn方法中的集合 我想要Java 莫基托-当。。。然后通过传递期望值列表多次返回,java,mockito,powermockito,Java,Mockito,Powermockito,为什么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, ca
// 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);
因为ExpectedValue的计数是任意的。thenReturn方法支持varargs,但不支持集合:
Mockito.when(mockObject.someMethod()).thenReturn(something1, something2, ...);
OR
Mockito.when(mockObject.someMethod()).thenReturn(something, arrayOfSomething);
另一种方法是链接返回调用:
两者都将在第一次调用mockObject.someMethod时返回something1,在第二次调用时返回something2,等等。然后返回的方法支持varargs,但不支持集合:
Mockito.when(mockObject.someMethod()).thenReturn(something1, something2, ...);
OR
Mockito.when(mockObject.someMethod()).thenReturn(something, arrayOfSomething);
另一种方法是链接返回调用:
两者都将在第一次调用mockObject.someMethod时返回something1,在第二次调用时返回something2,等等。mockObject.someMethod是否返回列表?@SteveBenett否,它返回一个Something对象。更新了问题。检查。我想这正是你想要的。@MensurQulami这只有在我知道期望值的计数时才有效。如果我不知道期望值的计数呢?我的意思是如果你想把一个列表作为参数传递,你不应该知道期望值的确切数目吗?我不确定我是否理解你的意思。mockObject.someMethod是否返回一个列表?@SteveBenett不,它返回一个Something对象。更新了问题。检查。我想这正是你想要的。@MensurQulami这只有在我知道期望值的计数时才有效。如果我不知道期望值的计数呢?我的意思是如果你想把一个列表作为参数传递,你不应该知道期望值的确切数目吗?我不确定我是否理解你的意思。Mockito.whenmockObject.someMethod.then返回某物,arrayOfSomething;这正是我想要的。谢谢Mockito.whenmockObject.someMethod.then返回something,arrayOfSomething;这正是我想要的。谢谢