是否有一种方法可以让存根方法在后续调用时返回不同的对象?我希望这样做是为了测试来自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 (...) { }        
}

当前回答

BDD风格:

import static org.mockito.BDDMockito.given;
        ...

        given(yourMock.yourMethod()).willReturn(1, 2, 3);

经典的风格:

import static org.mockito.Mockito.when;
        ...

        when(yourMock.yourMethod()).thenReturn(1, 2, 3);

明确的风格:

        ...

        when(yourMock.yourMethod())
            .thenReturn(1)
            .thenReturn(2)
            .thenReturn(3);

取决于参数

假设我们有2个参数,检查第2个(列表)参数的大小:

        ...

        when(yourMock.yourMethod(any(), anyList()))
             .thenAnswer(args -> ((List) args.getArgument(1)).size() < 2
                                 ? 1
                                 : 3);

arg是对象,所以我们必须将arg转换为我们的类型。在我的例子中,我转换了^^^ to (List)。

BDD

        ...

        given(yourMock.yourMethod(any(), anyList()))
             .willAnswer(args -> ((List) args.getArgument(1)).size() < 2
                                 ? 1
                                 : 3);

其他回答

正如前面指出的,几乎所有的调用都是可链的。

所以你可以打电话

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();

更多信息在Mockito的文档。

几乎所有的调用都是可链的:

doReturn(null).doReturn(anotherInstance).when(mock).method();

doReturn(value1, value2, value3)。When(方法调用)

BDD风格:

import static org.mockito.BDDMockito.given;
        ...

        given(yourMock.yourMethod()).willReturn(1, 2, 3);

经典的风格:

import static org.mockito.Mockito.when;
        ...

        when(yourMock.yourMethod()).thenReturn(1, 2, 3);

明确的风格:

        ...

        when(yourMock.yourMethod())
            .thenReturn(1)
            .thenReturn(2)
            .thenReturn(3);

取决于参数

假设我们有2个参数,检查第2个(列表)参数的大小:

        ...

        when(yourMock.yourMethod(any(), anyList()))
             .thenAnswer(args -> ((List) args.getArgument(1)).size() < 2
                                 ? 1
                                 : 3);

arg是对象,所以我们必须将arg转换为我们的类型。在我的例子中,我转换了^^^ to (List)。

BDD

        ...

        given(yourMock.yourMethod(any(), anyList()))
             .willAnswer(args -> ((List) args.getArgument(1)).size() < 2
                                 ? 1
                                 : 3);

与8年前@[Igor Nikolaev]的答案相关,使用Java 8中的lambda表达式可以在一定程度上简化answer的使用。

when(someMock.someMethod()).thenAnswer(invocation -> {
    doStuff();
    return;
});

或者更简单地说:

when(someMock.someMethod()).thenAnswer(invocation -> doStuff());