Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Java by (10.2k points)

Is there a way to have a stubbed method return different objects on subsequent invocations? I'd like to do this to test nondeterminate responses from an ExecutorCompletionService. i.e. to test that irrespective of the return order of the methods, the outcome remains constant.

The code I'm looking to test looks something like this.

// 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 (...) { }        

}

2 Answers

0 votes
by (46k points)

You can do that by practicing the thenAnswer method (when chaining with when):

when(someMock.someMethod()).thenAnswer(new Answer() {

    private int count = 0;

    public Object answer(InvocationOnMock invocation) {

        if (count++ == 1)

            return 1;

        return 2;

    }

});

Or by applying the equivalent, static doAnswer method:

doAnswer(new Answer() {

    private int count = 0;

    public Object answer(InvocationOnMock invocation) {

        if (count++ == 1)

            return 1;

        return 2;

    }

}).when(someMock).someMethod();

0 votes
by (1.9k points)
One may easily stub a method in the Mockito mocking framework which would return different objects at different calls. This enables modelling different reactions to every one of the calls to a stubbed method. Chaining few thenReturn() calls and then return() along with a multiple-argument call to achieve the same are available options.

The following example demonstrates how to configure a mocked method to return different objects in subsequent calls:


import static org.mockito.Mockito.*;
import java.util.concurrent.*;

public class TestExample {
    public static void main(String[] args) throws Exception {
        // Create a mock of Future<T>
        Future<String> future1 = mock(Future.class);
        Future<String> future2 = mock(Future.class);

        // Stub the 'get()' method of the futures to return different values
        when(future1.get()).thenReturn("Result 1");
        when(future2.get()).thenReturn("Result 2");

        // Create a mock ExecutorCompletionService
        ExecutorCompletionService<String> completionService = mock(ExecutorCompletionService.class);

        // Stub the 'take()' method to return different Future objects on subsequent calls
        when(completionService.take()).thenReturn(future1).thenReturn(future2);

        // Invoke the method you want to test and verify behavior
        String result1 = completionService.take().get();
        String result2 = completionService.take().get();

        System.out.println(result1); // Outputs: Result 1
        System.out.println(result2); // Outputs: Result 2
    }
}

Related questions

1.2k questions

2.7k answers

501 comments

693 users

Browse Categories

...