1package org.mockito.internal.junit;
2
3import org.junit.Test;
4import org.mockito.Mock;
5import org.mockitousage.IMethods;
6import org.mockitoutil.TestBase;
7
8import java.util.List;
9
10import static java.util.Arrays.asList;
11import static org.junit.Assert.*;
12import static org.mockito.Mockito.when;
13
14public class ArgMismatchFinderTest extends TestBase {
15
16    ArgMismatchFinder finder = new ArgMismatchFinder();
17    @Mock IMethods mock1;
18    @Mock IMethods mock2;
19
20    @Test
21    public void no_interactions() throws Exception {
22        //when
23        StubbingArgMismatches mismatches = finder.getStubbingArgMismatches((List) asList(mock1, mock2));
24
25        //then
26        assertEquals(0, mismatches.size());
27    }
28
29    @Test
30    public void no_mismatch_when_mock_different() throws Exception {
31        //given
32        when(mock1.simpleMethod(1)).thenReturn("1");
33        mock2.simpleMethod(2); //arg mismatch on different mock
34
35        //when
36        StubbingArgMismatches mismatches = finder.getStubbingArgMismatches((List) asList(mock1, mock2));
37
38        //then
39        assertEquals(0, mismatches.size());
40    }
41
42    @Test
43    public void no_mismatch_when_method_different() throws Exception {
44        //given
45        when(mock1.simpleMethod(1)).thenReturn("1");
46        mock1.otherMethod();
47
48        //when
49        StubbingArgMismatches mismatches = finder.getStubbingArgMismatches((List) asList(mock1, mock2));
50
51        //then
52        assertEquals(0, mismatches.size());
53    }
54
55    @Test
56    public void no_mismatch_when_stubbing_used() throws Exception {
57        //given
58        when(mock1.simpleMethod(1)).thenReturn("1");
59        mock1.simpleMethod(1); // stub used
60        mock1.simpleMethod(2); // no stubbing, but we don't want it to be reported, either
61
62        //when
63        StubbingArgMismatches mismatches = finder.getStubbingArgMismatches((List) asList(mock1, mock2));
64
65        //then
66        assertEquals(0, mismatches.size());
67    }
68
69    @Test
70    public void stubbing_mismatch() throws Exception {
71        //given
72        when(mock1.simpleMethod(1)).thenReturn("1");
73        mock1.simpleMethod(2);
74
75        //when
76        StubbingArgMismatches mismatches = finder.getStubbingArgMismatches((List) asList(mock1, mock2));
77
78        //then
79        assertEquals(1, mismatches.size());
80    }
81
82    @Test
83    public void single_mismatch_with_multiple_invocations() throws Exception {
84        //given
85        when(mock1.simpleMethod(1)).thenReturn("1");
86        mock1.simpleMethod(2);
87        mock1.simpleMethod(3);
88
89        //when
90        StubbingArgMismatches mismatches = finder.getStubbingArgMismatches((List) asList(mock1, mock2));
91
92        //then
93        assertEquals(1, mismatches.size());
94        assertEquals("{mock1.simpleMethod(1);=[mock1.simpleMethod(2);, mock1.simpleMethod(3);]}", mismatches.toString());
95    }
96
97    @Test
98    public void single_invocation_with_multiple_stubs() throws Exception {
99        //given
100        when(mock1.simpleMethod(1)).thenReturn("1");
101        when(mock1.simpleMethod(2)).thenReturn("2");
102        mock1.simpleMethod(3);
103
104        //when
105        StubbingArgMismatches mismatches = finder.getStubbingArgMismatches((List) asList(mock1, mock2));
106
107        //then
108        assertEquals(2, mismatches.size());
109        assertEquals("{mock1.simpleMethod(1);=[mock1.simpleMethod(3);], mock1.simpleMethod(2);=[mock1.simpleMethod(3);]}"
110                , mismatches.toString());
111    }
112
113    @Test
114    public void mismatch_reports_only_unstubbed_invocations() throws Exception {
115        //given
116        when(mock1.simpleMethod(1)).thenReturn("1"); //unused
117        when(mock1.simpleMethod(2)).thenReturn("2"); //used
118        mock1.simpleMethod(2); //stubbed
119        mock1.simpleMethod(3); //unstubbed
120
121        //when
122        StubbingArgMismatches mismatches = finder.getStubbingArgMismatches((List) asList(mock1, mock2));
123
124        //then
125        assertEquals("{mock1.simpleMethod(1);=[mock1.simpleMethod(3);]}", mismatches.toString());
126    }
127}
128