Java Code Examples for org.mockito.stubbing.OngoingStubbing#thenReturn()

The following examples show how to use org.mockito.stubbing.OngoingStubbing#thenReturn() . 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: GitHubPRRepositoryTest.java    From github-integration-plugin with MIT License 6 votes vote down vote up
private void getAllPrBuildsCommonExpectations(int size) {
    when(job.getBuilds()).thenReturn(builds);
    when(builds.size()).thenReturn(size);
    when(job.getParent()).thenReturn(itemGroup);
    when(itemGroup.getFullName()).thenReturn("JobName");

    when(builds.iterator()).thenReturn(iterator);

    OngoingStubbing<Boolean> hasNextExpectation = size >= 1 ?
            when(iterator.hasNext()).thenReturn(true) : when(iterator.hasNext()).thenReturn(false);
    for (int i = 1; i < size; i++) {
        hasNextExpectation.thenReturn(true);
    }
    hasNextExpectation.thenReturn(false);

    OngoingStubbing<Object> nextExpectation = when(iterator.next()).thenReturn(run);
    for (int i = 1; i < size; i++) {
        nextExpectation.thenReturn(run);
    }
}
 
Example 2
Source File: PrivateResourceDetectorTest.java    From javaide with GNU General Public License v3.0 6 votes vote down vote up
public static AndroidLibrary createMockLibrary(String allResources, String publicResources,
        List<AndroidLibrary> dependencies)
        throws IOException {
    final File tempDir = TestUtils.createTempDirDeletedOnExit();

    Files.write(allResources, new File(tempDir, FN_RESOURCE_TEXT), Charsets.UTF_8);
    File publicTxtFile = new File(tempDir, FN_PUBLIC_TXT);
    if (publicResources != null) {
        Files.write(publicResources, publicTxtFile, Charsets.UTF_8);
    }
    AndroidLibrary library = mock(AndroidLibrary.class);
    when(library.getPublicResources()).thenReturn(publicTxtFile);

    // Work around wildcard capture
    //when(mock.getLibraryDependencies()).thenReturn(dependencies);
    List libraryDependencies = library.getLibraryDependencies();
    OngoingStubbing<List> setter = when(libraryDependencies);
    setter.thenReturn(dependencies);
    return library;
}
 
Example 3
Source File: JsonRpc2_0RxTest.java    From etherscan-explorer with GNU General Public License v3.0 5 votes vote down vote up
@Test
public void testReplayBlocksDescendingObservable() throws Exception {

    List<EthBlock> ethBlocks = Arrays.asList(createBlock(2), createBlock(1), createBlock(0));

    OngoingStubbing<EthBlock> stubbing =
            when(web3jService.send(any(Request.class), eq(EthBlock.class)));
    for (EthBlock ethBlock : ethBlocks) {
        stubbing = stubbing.thenReturn(ethBlock);
    }

    Observable<EthBlock> observable = web3j.replayBlocksObservable(
            new DefaultBlockParameterNumber(BigInteger.ZERO),
            new DefaultBlockParameterNumber(BigInteger.valueOf(2)),
            false, false);

    CountDownLatch transactionLatch = new CountDownLatch(ethBlocks.size());
    CountDownLatch completedLatch = new CountDownLatch(1);

    List<EthBlock> results = new ArrayList<>(ethBlocks.size());
    Subscription subscription = observable.subscribe(
            result -> {
                results.add(result);
                transactionLatch.countDown();
            },
            throwable -> fail(throwable.getMessage()),
            () -> completedLatch.countDown());

    transactionLatch.await(1, TimeUnit.SECONDS);
    assertThat(results, equalTo(ethBlocks));

    subscription.unsubscribe();

    completedLatch.await(1, TimeUnit.SECONDS);
    assertTrue(subscription.isUnsubscribed());
}
 
Example 4
Source File: BaseStubbing.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
public OngoingStubbing<T> thenReturn(T value, T... values) {
    OngoingStubbing<T> stubbing = thenReturn(value);            
    if (values == null) {
        return stubbing.thenReturn(null);
    }
    for (T v: values) {
        stubbing = stubbing.thenReturn(v);
    }
    return stubbing;
}
 
Example 5
Source File: GPDBWritableTest.java    From pxf with Apache License 2.0 5 votes vote down vote up
private DataInput buildStream(int[] data, boolean throwException) throws Exception {
    inputStream = mock(DataInput.class);
    OngoingStubbing<Integer> ongoing = when(inputStream.readInt());
    for (int b : data) {
        ongoing = ongoing.thenReturn(b);
    }

    if (throwException) {
        ongoing.thenThrow(new EOFException());
    }
    return inputStream;
}
 
Example 6
Source File: SearchTransformTest.java    From heroic with Apache License 2.0 5 votes vote down vote up
public void setSearchHitPages(SearchHit[]... pages) {
    OngoingStubbing<SearchHit[]> stub = when(searchHits.getHits());

    for (final SearchHit[] page : pages) {
        stub = stub.thenReturn(page);
    }
}
 
Example 7
Source File: GitHubPRRepositoryTest.java    From github-integration-plugin with MIT License 5 votes vote down vote up
private void getAllPrBuildsNonNullCauseExpectations(int size) {
    when(run.getCause(GitHubPRCause.class)).thenReturn(cause);
    OngoingStubbing<Integer> expectation = null;
    for (int i = 0; i < size; i++) {
        if (expectation == null) {
            expectation = when(cause.getNumber()).thenReturn(i);
        } else {
            expectation.thenReturn(i);
        }
    }
}
 
Example 8
Source File: JedisClientTest.java    From logback-redis with Apache License 2.0 5 votes vote down vote up
private void withClientOnTryNumber(int tries) {
    OngoingStubbing<Optional<Jedis>> stub = when(clientProvider.getJedisClient());
    for (int i = 1; i < tries; i++) {
        stub = stub.thenReturn(Optional.empty());
    }
    stub.thenReturn(Optional.of(jedis));
}
 
Example 9
Source File: TestPutKudu.java    From nifi with Apache License 2.0 4 votes vote down vote up
private static <T> void stubSlices(OngoingStubbing<T> stubbing, List<T> slices) {
    for (T slice : slices) {
        stubbing = stubbing.thenReturn(slice);
    }
}
 
Example 10
Source File: LineMessagingClientImplTest.java    From line-bot-sdk-java with Apache License 2.0 4 votes vote down vote up
private static <T> void whenCall(Call<T> call, T value) {
    final OngoingStubbing<Call<T>> callOngoingStubbing = when(call);
    callOngoingStubbing.thenReturn(enqueue(value));
}
 
Example 11
Source File: LineBlobClientImplTest.java    From line-bot-sdk-java with Apache License 2.0 4 votes vote down vote up
private static <T> void whenCall(Call<T> call, T value) {
    final OngoingStubbing<Call<T>> callOngoingStubbing = when(call);
    callOngoingStubbing.thenReturn(enqueue(value));
}
 
Example 12
Source File: FootballTest.java    From codenjoy with GNU General Public License v3.0 4 votes vote down vote up
private void dice(int...ints) {
    OngoingStubbing<Integer> when = when(dice.next(anyInt()));
    for (int i : ints) {
        when = when.thenReturn(i);
    }
}
 
Example 13
Source File: LoderunnerTest.java    From codenjoy with GNU General Public License v3.0 4 votes vote down vote up
private void dice(int...ints) {
    OngoingStubbing<Integer> when = when(dice.next(anyInt()));
    for (int i : ints) {
        when = when.thenReturn(i);
    }
}
 
Example 14
Source File: ChessTest.java    From codenjoy with GNU General Public License v3.0 4 votes vote down vote up
private void dice(int...ints) {
    OngoingStubbing<Integer> when = when(dice.next(anyInt()));
    for (int i : ints) {
        when = when.thenReturn(i);
    }
}
 
Example 15
Source File: SampleTest.java    From codenjoy with GNU General Public License v3.0 4 votes vote down vote up
private void dice(int...ints) {
    OngoingStubbing<Integer> when = when(dice.next(anyInt()));
    for (int i : ints) {
        when = when.thenReturn(i);
    }
}
 
Example 16
Source File: PongTest.java    From codenjoy with GNU General Public License v3.0 4 votes vote down vote up
private void dice(int...ints) {
    OngoingStubbing<Integer> when = when(dice.next(anyInt()));
    for (int i : ints) {
        when = when.thenReturn(i);
    }
}
 
Example 17
Source File: StartAndJumpTest.java    From codenjoy with GNU General Public License v3.0 4 votes vote down vote up
private void dice(int... ints) {
    OngoingStubbing<Integer> when = when(dice.next(anyInt()));
    for (int i : ints) {
        when = when.thenReturn(i);
    }
}
 
Example 18
Source File: SokobanTestBackup.java    From codenjoy with GNU General Public License v3.0 4 votes vote down vote up
private void dice(int...ints) {
    OngoingStubbing<Integer> when = when(dice.next(anyInt()));
    for (int i : ints) {
        when = when.thenReturn(i);
    }
}
 
Example 19
Source File: TetrisTest.java    From codenjoy with GNU General Public License v3.0 4 votes vote down vote up
private void dice(int...ints) {
    OngoingStubbing<Integer> when = when(dice.next(anyInt()));
    for (int i : ints) {
        when = when.thenReturn(i);
    }
}
 
Example 20
Source File: JsonRpc2_0RxTest.java    From client-sdk-java with Apache License 2.0 4 votes vote down vote up
@Test
public void testCatchUpToLatestAndSubscribeToNewBlockObservable() throws Exception {
    List<PlatonBlock> expected = Arrays.asList(
            createBlock(0), createBlock(1), createBlock(2),
            createBlock(3), createBlock(4), createBlock(5),
            createBlock(6));

    List<PlatonBlock> ethBlocks = Arrays.asList(
            expected.get(2),  // greatest block
            expected.get(0), expected.get(1), expected.get(2),
            expected.get(4), // greatest block
            expected.get(3), expected.get(4),
            expected.get(4),  // greatest block
            expected.get(5),  // initial response from platonGetFilterLogs call
            expected.get(6)); // subsequent block from new block observable

    OngoingStubbing<PlatonBlock> stubbing =
            when(web3jService.send(any(Request.class), eq(PlatonBlock.class)));
    for (PlatonBlock ethBlock : ethBlocks) {
        stubbing = stubbing.thenReturn(ethBlock);
    }

    PlatonFilter ethFilter = objectMapper.readValue(
            "{\n"
                    + "  \"id\":1,\n"
                    + "  \"jsonrpc\": \"2.0\",\n"
                    + "  \"result\": \"0x1\"\n"
                    + "}", PlatonFilter.class);
    PlatonLog ethLog = objectMapper.readValue(
            "{\"jsonrpc\":\"2.0\",\"id\":1,\"result\":["
                    + "\"0x31c2342b1e0b8ffda1507fbffddf213c4b3c1e819ff6a84b943faabb0ebf2403\""
                    + "]}",
            PlatonLog.class);
    PlatonUninstallFilter ethUninstallFilter = objectMapper.readValue(
            "{\"jsonrpc\":\"2.0\",\"id\":1,\"result\":true}", PlatonUninstallFilter.class);

    when(web3jService.send(any(Request.class), eq(PlatonFilter.class)))
            .thenReturn(ethFilter);
    when(web3jService.send(any(Request.class), eq(PlatonLog.class)))
            .thenReturn(ethLog);
    when(web3jService.send(any(Request.class), eq(PlatonUninstallFilter.class)))
            .thenReturn(ethUninstallFilter);

    Observable<PlatonBlock> observable = web3j.catchUpToLatestAndSubscribeToNewBlocksObservable(
            new DefaultBlockParameterNumber(BigInteger.ZERO),
            false);

    CountDownLatch transactionLatch = new CountDownLatch(expected.size());
    CountDownLatch completedLatch = new CountDownLatch(1);

    List<PlatonBlock> results = new ArrayList<>(expected.size());
    Subscription subscription = observable.subscribe(
            result -> {
                results.add(result);
                transactionLatch.countDown();
            },
            throwable -> fail(throwable.getMessage()),
            () -> completedLatch.countDown());

    transactionLatch.await(1250, TimeUnit.MILLISECONDS);
    assertThat(results, equalTo(expected));

    subscription.unsubscribe();

    completedLatch.await(1, TimeUnit.SECONDS);
    assertTrue(subscription.isUnsubscribed());
}