Java Code Examples for org.mockito.Mockito#when()

The following examples show how to use org.mockito.Mockito#when() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: HealthCheckedChannelPoolTest.java    From aws-sdk-java-v2 with Apache License 2.0 5 votes vote down vote up
public void stubAcquireHealthySequence(Boolean... acquireHealthySequence) {
    OngoingStubbing<Future<Channel>> stubbing = Mockito.when(downstreamChannelPool.acquire(any()));
    for (boolean shouldAcquireBeHealthy : acquireHealthySequence) {
        stubbing = stubbing.thenAnswer(invocation -> {
            Promise<Channel> promise = invocation.getArgumentAt(0, Promise.class);
            Channel channel = Mockito.mock(Channel.class);
            Mockito.when(channel.isActive()).thenReturn(shouldAcquireBeHealthy);
            stubKeepAliveAttribute(channel, null);
            channels.add(channel);
            promise.setSuccess(channel);
            return promise;
        });
    }
}
 
Example 2
Source File: HealthCheckedChannelPoolTest.java    From aws-sdk-java-v2 with Apache License 2.0 5 votes vote down vote up
private void stubAcquireActiveAndKeepAlive() {
    OngoingStubbing<Future<Channel>> stubbing = Mockito.when(downstreamChannelPool.acquire(any()));
    stubbing = stubbing.thenAnswer(invocation -> {
        Promise<Channel> promise = invocation.getArgumentAt(0, Promise.class);
        Channel channel = Mockito.mock(Channel.class);
        Mockito.when(channel.isActive()).thenReturn(true);

        stubKeepAliveAttribute(channel, true);

        channels.add(channel);
        promise.setSuccess(channel);
        return promise;
    });
}
 
Example 3
Source File: HereAccountTest.java    From here-aaa-java-sdk with Apache License 2.0 5 votes vote down vote up
/**
 * Build a mock HttpProvider that always returns the provided response body.
 */
private HttpProvider mockHttpProvider(HttpResponse... responses) throws Exception {
    HttpProvider mock = Mockito.mock(HttpProvider.class);
    OngoingStubbing<HttpResponse> stub = Mockito.when(mock.execute(Mockito.any()));
    for (HttpResponse response : responses) {
        stub = stub.thenReturn(response);
    }
    return mock;
}
 
Example 4
Source File: BulkProcessorTest.java    From elasticsearch-hadoop with Apache License 2.0 5 votes vote down vote up
private RestClient mockClientResponses(RestClient.BulkActionResponse... responses) {
    RestClient mockClient = Mockito.mock(RestClient.class);

    OngoingStubbing<RestClient.BulkActionResponse> stubb = Mockito.when(
            mockClient.bulk(Mockito.eq(resource), Mockito.any(TrackingBytesArray.class))
    );

    for (RestClient.BulkActionResponse response : responses) {
        stubb = stubb.thenReturn(response);
    }

    stubb.thenThrow(new AssertionError("Exhausted all given test responses."));

    return mockClient;
}
 
Example 5
Source File: RetryingHttpClientExecutorTest.java    From salt-step with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
protected void setupResponseWithStatusLine(int... codes) {
    StatusLine line = Mockito.mock(StatusLine.class);
    OngoingStubbing<Integer> stub = Mockito.when(line.getStatusCode());
    for (int code : codes) {
        stub = stub.thenReturn(code);
    }
    Mockito.when(response.getStatusLine()).thenReturn(line);
}
 
Example 6
Source File: WithMockito.java    From Patterdale with Apache License 2.0 4 votes vote down vote up
default <T> OngoingStubbing<T> when(T methodCall) {
    return Mockito.when(methodCall);
}
 
Example 7
Source File: WithMockito.java    From mockito-java8 with Apache License 2.0 4 votes vote down vote up
/**
 * Delegates call to {@link Mockito#when(Object)}.
 */
default <T> OngoingStubbing<T> when(T methodCall) {
    return Mockito.when(methodCall);
}
 
Example 8
Source File: TestCommons.java    From rapidoid with Apache License 2.0 4 votes vote down vote up
protected <T> OngoingStubbing<T> when(T methodCall) {
	return Mockito.when(methodCall);
}