How to use method of org.mockito.internal.AllInvocationsFinderTest class

Best Mockito code snippet using org.mockito.internal.AllInvocationsFinderTest.

Run Mockito automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

copy
1/*
2 * Copyright (c) 2007 Mockito contributors
3 * This program is made available under the terms of the MIT License.
4 */
5
6package org.mockito.internal;
7
8import org.junit.Before;
9import org.junit.Test;
10import org.mockito.stubbing.Stubbing;
11import org.mockito.invocation.Invocation;
12import org.mockitousage.IMethods;
13import org.mockitoutil.TestBase;
14
15import java.util.ArrayList;
16import java.util.List;
17
18import static java.util.Arrays.asList;
19import static org.junit.Assert.assertEquals;
20import static org.junit.Assert.assertTrue;
21import static org.mockito.Mockito.mock;
22import static org.mockito.Mockito.when;
23import static org.mockito.internal.invocation.finder.AllInvocationsFinder.find;
24import static org.mockito.internal.invocation.finder.AllInvocationsFinder.findStubbings;
25
26public class AllInvocationsFinderTest extends TestBase {
27
28    private IMethods mockTwo;
29    private IMethods mockOne;
30
31    @Before
32    public void setup() {
33        mockOne = mock(IMethods.class);
34        mockTwo = mock(IMethods.class);
35    }
36
37    @Test
38    public void no_interactions() throws Exception {
39        //expect
40        assertTrue(find(asList(mockOne, mockTwo)).isEmpty());
41        assertTrue(findStubbings(asList(mockOne, mockTwo)).isEmpty());
42    }
43
44    @Test
45    public void provides_invocations_in_order() throws Exception {
46        //given
47        mockOne.simpleMethod(100);
48        mockTwo.simpleMethod(200);
49        mockOne.simpleMethod(300);
50
51        //when
52        List<Invocation> invocations = find(asList(mockOne, mockTwo));
53
54        //then
55        assertEquals(3, invocations.size());
56        assertArgumentEquals(100, invocations.get(0));
57        assertArgumentEquals(200, invocations.get(1));
58        assertArgumentEquals(300, invocations.get(2));
59    }
60
61    @Test
62    public void deduplicates_interactions_from_the_same_mock() throws Exception {
63        //given
64        mockOne.simpleMethod(100);
65
66        //when
67        List<Invocation> invocations = find(asList(mockOne, mockOne, mockOne));
68
69        //then
70        assertEquals(1, invocations.size());
71    }
72
73    @Test
74    public void provides_stubbings_in_order() throws Exception {
75        //given
76        mockOne.simpleMethod(50); //ignored, not a stubbing
77        when(mockOne.simpleMethod(100)).thenReturn("100");
78        when(mockOne.simpleMethod(200)).thenReturn("200");
79        when(mockTwo.simpleMethod(300)).thenReturn("300");
80
81        //when
82        List<Stubbing> stubbings = new ArrayList<Stubbing>(findStubbings(asList(mockOne, mockOne, mockTwo)));
83
84        //then
85        assertEquals(3, stubbings.size());
86        assertArgumentEquals(100, stubbings.get(0).getInvocation());
87        assertArgumentEquals(200, stubbings.get(1).getInvocation());
88        assertArgumentEquals(300, stubbings.get(2).getInvocation());
89    }
90
91    private void assertArgumentEquals(Object argumentValue, Invocation invocation) {
92        assertEquals(argumentValue, invocation.getArgument(0));
93    }
94}
95
Full Screen

Accelerate Your Automation Test Cycles With LambdaTest

Leverage LambdaTest’s cloud-based platform to execute your automation tests in parallel and trim down your test execution time significantly. Your first 100 automation testing minutes are on us.

Try LambdaTest
LambdaTestX

We use cookies to give you the best experience. Cookies help to provide a more personalized experience and relevant advertising for you, and web analytics for us. Learn More in our Cookies policy, Privacy & Terms of service

Allow Cookie
Sarah

I hope you find the best code examples for your project.

If you want to accelerate automated browser testing, try LambdaTest. Your first 100 automation testing minutes are FREE.

Sarah Elson (Product & Growth Lead)