• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2017 Mockito contributors
3  * This program is made available under the terms of the MIT License.
4  */
5 package org.mockitousage.internal.junit;
6 
7 import org.junit.Test;
8 import org.mockito.Mock;
9 import org.mockito.internal.junit.UnusedStubbings;
10 import org.mockito.internal.junit.UnusedStubbingsFinder;
11 import org.mockitousage.IMethods;
12 import org.mockitoutil.TestBase;
13 
14 import java.util.Collection;
15 import java.util.List;
16 
17 import static java.util.Arrays.asList;
18 import static org.junit.Assert.assertEquals;
19 import static org.mockito.Mockito.lenient;
20 import static org.mockito.Mockito.when;
21 
22 /**
23  * This unit test lives in 'org.mockitousage' package for a reason.
24  * It makes it easy to write tests that depend on the stack trace filtering logic.
25  * Long term, it would be nice to have more configurability in TestBase
26  *  to make it easy to write such unit tests in 'org.mockito.*' packages.
27  */
28 public class UnusedStubbingsFinderTest extends TestBase {
29 
30     UnusedStubbingsFinder finder = new UnusedStubbingsFinder();
31     @Mock IMethods mock1;
32     @Mock IMethods mock2;
33 
34     @Test
no_interactions()35     public void no_interactions() throws Exception {
36         //expect
37         assertEquals(0, finder.getUnusedStubbings((List) asList(mock1, mock2)).size());
38         assertEquals(0, finder.getUnusedStubbingsByLocation((List) asList(mock1, mock2)).size());
39     }
40 
41     @Test
no_stubbings()42     public void no_stubbings() throws Exception {
43         //when
44         mock1.simpleMethod();
45 
46         //then
47         assertEquals(0, finder.getUnusedStubbings((List) asList(mock1, mock2)).size());
48         assertEquals(0, finder.getUnusedStubbingsByLocation((List) asList(mock1, mock2)).size());
49     }
50 
51     @Test
no_unused_stubbings()52     public void no_unused_stubbings() throws Exception {
53         //when
54         when(mock1.simpleMethod()).thenReturn("1");
55         mock1.simpleMethod();
56 
57         //then
58         assertEquals(0, finder.getUnusedStubbings((List) asList(mock1, mock2)).size());
59         assertEquals(0, finder.getUnusedStubbingsByLocation((List) asList(mock1, mock2)).size());
60     }
61 
62     @Test
unused_stubbings()63     public void unused_stubbings() throws Exception {
64         //when
65         when(mock1.simpleMethod()).thenReturn("1");
66 
67         //then
68         assertEquals(1, finder.getUnusedStubbings((List) asList(mock1, mock2)).size());
69         assertEquals(1, finder.getUnusedStubbingsByLocation((List) asList(mock1, mock2)).size());
70     }
71 
72     @Test
some_unused_stubbings()73     public void some_unused_stubbings() throws Exception {
74         when(mock1.simpleMethod(1)).thenReturn("1");
75         when(mock2.simpleMethod(2)).thenReturn("2");
76         when(mock2.simpleMethod(3)).thenReturn("3");
77 
78         mock2.simpleMethod(2);
79 
80         //when
81         UnusedStubbings stubbings = finder.getUnusedStubbings((List) asList(mock1, mock2));
82 
83         //then
84         assertEquals(2, stubbings.size());
85         assertEquals("[mock1.simpleMethod(1); stubbed with: [Returns: 1], mock2.simpleMethod(3); stubbed with: [Returns: 3]]",
86                 stubbings.toString());
87     }
88 
89     @Test
unused_and_lenient_stubbings()90     public void unused_and_lenient_stubbings() throws Exception {
91         when(mock1.simpleMethod(1)).thenReturn("1");
92         when(mock1.simpleMethod(2)).thenReturn("2");
93         lenient().when(mock2.simpleMethod(3)).thenReturn("3");
94 
95         mock1.simpleMethod(1);
96 
97         //when
98         UnusedStubbings stubbings = finder.getUnusedStubbings((List) asList(mock1, mock2));
99 
100         //then
101         assertEquals(1, stubbings.size());
102         assertEquals("[mock1.simpleMethod(2); stubbed with: [Returns: 2]]",
103                 stubbings.toString());
104     }
105 
106     @Test
some_unused_stubbings_by_location()107     public void some_unused_stubbings_by_location() throws Exception {
108         when(mock1.simpleMethod(1)).thenReturn("1");
109         when(mock2.simpleMethod(2)).thenReturn("2");
110         when(mock2.simpleMethod(3)).thenReturn("3");
111         lenient().when(mock2.differentMethod()).thenReturn("4"); //will not be included in results
112 
113         mock2.simpleMethod(2);
114 
115         //when
116         Collection stubbings = finder.getUnusedStubbingsByLocation((List) asList(mock1, mock2));
117 
118         //then
119         assertEquals(2, stubbings.size());
120         assertEquals("[mock1.simpleMethod(1);, mock2.simpleMethod(3);]", stubbings.toString());
121     }
122 
123     @Test
stubbing_used_by_location()124     public void stubbing_used_by_location() throws Exception {
125         //when
126         //Emulating stubbing in the same location by putting stubbing in the same line:
127         when(mock1.simpleMethod(1)).thenReturn("1"); when(mock2.simpleMethod(1)).thenReturn("1");
128         //End of emulation
129         mock1.simpleMethod(1);
130 
131         //then technically unused stubbings exist
132         assertEquals(1, finder.getUnusedStubbings((List) asList(mock1, mock2)).size());
133         //however if we consider stubbings in the same location as the same stubbing, all is used:
134         assertEquals(0, finder.getUnusedStubbingsByLocation((List) asList(mock1, mock2)).size());
135     }
136 
137     @Test
deduplicates_stubbings_by_location()138     public void deduplicates_stubbings_by_location() throws Exception {
139         //when
140         //Emulating stubbing in the same location by putting stubbing in the same line:
141         when(mock1.simpleMethod(1)).thenReturn("1"); when(mock2.simpleMethod(1)).thenReturn("1");
142         //End of emulation
143 
144         //when
145         Collection stubbings = finder.getUnusedStubbingsByLocation((List) asList(mock1, mock2));
146 
147         //then
148         assertEquals(1, stubbings.size());
149     }
150 }
151