Emm*_*mma 254 java mocking mockito
有没有办法让stubbed方法在后续调用中返回不同的对象?我想这样做来测试一个非确定的响应ExecutorCompletionService.即,无论方法的返回顺序如何进行测试,结果都保持不变.
我想要测试的代码看起来像这样.
// Create an completion service so we can group these tasks together
ExecutorCompletionService<T> completionService =
new ExecutorCompletionService<T>(service);
// Add all these tasks to the completion service
for (Callable<T> t : ts)
completionService.submit(request);
// As an when each call finished, add it to the response set.
for (int i = 0; i < calls.size(); i ++) {
try {
T t = completionService.take().get();
// do some stuff that I want to test
} catch (...) { }
}
Run Code Online (Sandbox Code Playgroud)
Daw*_*ica 574
怎么样
when( method-call ).thenReturn( value1, value2, value3 );
Run Code Online (Sandbox Code Playgroud)
您可以在thenReturn的括号中添加任意数量的参数,前提是它们都是正确的类型.第一次调用方法时将返回第一个值,然后是第二个答案,依此类推.所有其他值用完后,将重复返回最后一个值.
Igo*_*aev 221
您可以使用该thenAnswer方法(链接时when):
when(someMock.someMethod()).thenAnswer(new Answer() {
private int count = 0;
public Object answer(InvocationOnMock invocation) {
if (count++ == 1)
return 1;
return 2;
}
});
Run Code Online (Sandbox Code Playgroud)
或者使用等效的静态doAnswer方法:
doAnswer(new Answer() {
private int count = 0;
public Object answer(InvocationOnMock invocation) {
if (count++ == 1)
return 1;
return 2;
}
}).when(someMock).someMethod();
Run Code Online (Sandbox Code Playgroud)
Ray*_*orm 131
如前所述,几乎所有的呼叫都是可链接的.
所以你可以打电话
when(mock.method()).thenReturn(foo).thenReturn(bar).thenThrow(new Exception("test"));
//OR if you're mocking a void method and/or using spy instead of mock
doReturn(foo).doReturn(bar).doThrow(new Exception("Test").when(mock).method();
Run Code Online (Sandbox Code Playgroud)
更多信息在Mockito的文件中.
Vol*_*bal 65
你甚至可以doReturn()像这样链接方法调用
doReturn(null).doReturn(anotherInstance).when(mock).method();
Run Code Online (Sandbox Code Playgroud)
可爱不是:)
epo*_*pox 19
import static org.mockito.BDDMockito.given;
...
given(yourMock.yourMethod()).willReturn(1, 2, 3);
Run Code Online (Sandbox Code Playgroud)
import static org.mockito.Mockito.when;
...
when(yourMock.yourMethod()).thenReturn(1, 2, 3);
Run Code Online (Sandbox Code Playgroud)
...
when(yourMock.yourMethod())
.thenReturn(1)
.thenReturn(2)
.thenReturn(3);
Run Code Online (Sandbox Code Playgroud)
小智 5
我已经实现了一个MultipleAnswer类,可以帮助我在每次调用中存根不同的答案。这是一段代码:
private final class MultipleAnswer<T> implements Answer<T> {
private final ArrayList<Answer<T>> mAnswers;
MultipleAnswer(Answer<T>... answer) {
mAnswers = new ArrayList<>();
mAnswers.addAll(Arrays.asList(answer));
}
@Override
public T answer(InvocationOnMock invocation) throws Throwable {
return mAnswers.remove(0).answer(invocation);
}
}
Run Code Online (Sandbox Code Playgroud)