• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2007 Mockito contributors
3  * This program is made available under the terms of the MIT License.
4  */
5 
6 package org.mockito.internal;
7 
8 import org.junit.Before;
9 import org.junit.Test;
10 import org.mockito.stubbing.Stubbing;
11 import org.mockito.invocation.Invocation;
12 import org.mockitousage.IMethods;
13 import org.mockitoutil.TestBase;
14 
15 import java.util.ArrayList;
16 import java.util.List;
17 
18 import static java.util.Arrays.asList;
19 import static junit.framework.TestCase.assertEquals;
20 import static org.junit.Assert.assertTrue;
21 import static org.mockito.Mockito.mock;
22 import static org.mockito.Mockito.when;
23 import static org.mockito.internal.invocation.finder.AllInvocationsFinder.find;
24 import static org.mockito.internal.invocation.finder.AllInvocationsFinder.findStubbings;
25 
26 public class AllInvocationsFinderTest extends TestBase {
27 
28     private IMethods mockTwo;
29     private IMethods mockOne;
30 
31     @Before
setup()32     public void setup() {
33         mockOne = mock(IMethods.class);
34         mockTwo = mock(IMethods.class);
35     }
36 
37     @Test
no_interactions()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
provides_invocations_in_order()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
deduplicates_interactions_from_the_same_mock()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
provides_stubbings_in_order()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 
assertArgumentEquals(Object argumentValue, Invocation invocation)91     private void assertArgumentEquals(Object argumentValue, Invocation invocation) {
92         assertEquals(argumentValue, invocation.getArgument(0));
93     }
94 }
95