• 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.After;
9 import org.junit.Test;
10 import org.mockito.Mock;
11 import org.mockito.StateMaster;
12 import org.mockito.exceptions.misusing.InvalidUseOfMatchersException;
13 import org.mockito.exceptions.misusing.UnfinishedStubbingException;
14 import org.mockito.exceptions.misusing.UnfinishedVerificationException;
15 import org.mockitousage.IMethods;
16 import org.mockitoutil.TestBase;
17 
18 import static org.junit.Assert.assertEquals;
19 import static org.junit.Assert.fail;
20 import static org.mockito.Mockito.*;
21 
22 /**
23  * invalid state happens if:
24  *
25  *    -unfinished stubbing
26  *    -unfinished doReturn()
27  *    -stubbing without actual method call
28  *    -verify without actual method call
29  *
30  * we should aim to detect invalid state in following scenarios:
31  *
32  *    -on method call on mock
33  *    -on verify
34  *    -on verifyZeroInteractions
35  *    -on verifyNoMoreInteractions
36  *    -on verify in order
37  *    -on stub
38  */
39 @SuppressWarnings({"unchecked", "deprecation"})
40 public class InvalidStateDetectionTest extends TestBase {
41 
42     @Mock private IMethods mock;
43 
44     @After
resetState()45     public void resetState() {
46         super.resetState();
47     }
48 
49     @SuppressWarnings({"CheckReturnValue", "MockitoUsage"})
50     @Test
shouldDetectUnfinishedStubbing()51     public void shouldDetectUnfinishedStubbing() {
52         when(mock.simpleMethod());
53         detectsAndCleansUp(new OnMethodCallOnMock(), UnfinishedStubbingException.class);
54 
55         when(mock.simpleMethod());
56         detectsAndCleansUp(new OnStub(), UnfinishedStubbingException.class);
57 
58         when(mock.simpleMethod());
59         detectsAndCleansUp(new OnVerify(), UnfinishedStubbingException.class);
60 
61         when(mock.simpleMethod());
62         detectsAndCleansUp(new OnVerifyInOrder(), UnfinishedStubbingException.class);
63 
64         when(mock.simpleMethod());
65         detectsAndCleansUp(new OnVerifyZeroInteractions(), UnfinishedStubbingException.class);
66 
67         when(mock.simpleMethod());
68         detectsAndCleansUp(new OnVerifyNoMoreInteractions(), UnfinishedStubbingException.class);
69 
70         when(mock.simpleMethod());
71         detectsAndCleansUp(new OnDoAnswer(), UnfinishedStubbingException.class);
72     }
73 
74     @SuppressWarnings({"CheckReturnValue", "MockitoUsage"})
75     @Test
shouldDetectUnfinishedDoAnswerStubbing()76     public void shouldDetectUnfinishedDoAnswerStubbing() {
77         doAnswer(null);
78         detectsAndCleansUp(new OnMethodCallOnMock(), UnfinishedStubbingException.class);
79 
80         doAnswer(null);
81         detectsAndCleansUp(new OnStub(), UnfinishedStubbingException.class);
82 
83         doAnswer(null);
84         detectsAndCleansUp(new OnVerify(), UnfinishedStubbingException.class);
85 
86         doAnswer(null);
87         detectsAndCleansUp(new OnVerifyInOrder(), UnfinishedStubbingException.class);
88 
89         doAnswer(null);
90         detectsAndCleansUp(new OnVerifyZeroInteractions(), UnfinishedStubbingException.class);
91 
92         doAnswer(null);
93         detectsAndCleansUp(new OnVerifyNoMoreInteractions(), UnfinishedStubbingException.class);
94 
95         doAnswer(null);
96         detectsAndCleansUp(new OnDoAnswer(), UnfinishedStubbingException.class);
97     }
98 
99     @SuppressWarnings({"CheckReturnValue", "MockitoUsage"})
100     @Test
shouldDetectUnfinishedVerification()101     public void shouldDetectUnfinishedVerification() {
102         verify(mock);
103         detectsAndCleansUp(new OnStub(), UnfinishedVerificationException.class);
104 
105         verify(mock);
106         detectsAndCleansUp(new OnVerify(), UnfinishedVerificationException.class);
107 
108         verify(mock);
109         detectsAndCleansUp(new OnVerifyInOrder(), UnfinishedVerificationException.class);
110 
111         verify(mock);
112         detectsAndCleansUp(new OnVerifyZeroInteractions(), UnfinishedVerificationException.class);
113 
114         verify(mock);
115         detectsAndCleansUp(new OnVerifyNoMoreInteractions(), UnfinishedVerificationException.class);
116 
117         verify(mock);
118         detectsAndCleansUp(new OnDoAnswer(), UnfinishedVerificationException.class);
119     }
120 
121     @Test
shouldDetectMisplacedArgumentMatcher()122     public void shouldDetectMisplacedArgumentMatcher() {
123         anyObject();
124         detectsAndCleansUp(new OnVerify(), InvalidUseOfMatchersException.class);
125 
126         anyObject();
127         detectsAndCleansUp(new OnVerifyInOrder(), InvalidUseOfMatchersException.class);
128 
129         anyObject();
130         detectsAndCleansUp(new OnVerifyZeroInteractions(), InvalidUseOfMatchersException.class);
131 
132         anyObject();
133         detectsAndCleansUp(new OnVerifyNoMoreInteractions(), InvalidUseOfMatchersException.class);
134 
135         anyObject();
136         detectsAndCleansUp(new OnDoAnswer(), InvalidUseOfMatchersException.class);
137     }
138 
139     @Test
shouldCorrectStateAfterDetectingUnfinishedStubbing()140     public void shouldCorrectStateAfterDetectingUnfinishedStubbing() {
141         doThrow(new RuntimeException()).when(mock);
142 
143         try {
144         	doThrow(new RuntimeException()).when(mock).oneArg(true);
145             fail();
146         } catch (UnfinishedStubbingException e) {}
147 
148         doThrow(new RuntimeException()).when(mock).oneArg(true);
149         try {
150             mock.oneArg(true);
151             fail();
152         } catch (RuntimeException e) {}
153     }
154 
155     @SuppressWarnings({"CheckReturnValue", "MockitoUsage"})
156     @Test
shouldCorrectStateAfterDetectingUnfinishedVerification()157     public void shouldCorrectStateAfterDetectingUnfinishedVerification() {
158         mock.simpleMethod();
159         verify(mock);
160 
161         try {
162             verify(mock).simpleMethod();
163             fail();
164         } catch (UnfinishedVerificationException e) {}
165 
166         verify(mock).simpleMethod();
167     }
168 
169     private interface DetectsInvalidState {
detect(IMethods mock)170         void detect(IMethods mock);
171     }
172 
173     private static class OnVerify implements DetectsInvalidState {
174         @SuppressWarnings({"CheckReturnValue", "MockitoUsage"})
detect(IMethods mock)175         public void detect(IMethods mock) {
176             verify(mock);
177         }
178     }
179 
180     private static class OnVerifyInOrder implements DetectsInvalidState {
181         @SuppressWarnings({"CheckReturnValue", "MockitoUsage"})
detect(IMethods mock)182         public void detect(IMethods mock) {
183             inOrder(mock).verify(mock);
184         }
185     }
186 
187     private static class OnVerifyZeroInteractions implements DetectsInvalidState {
188         @SuppressWarnings({"CheckReturnValue", "MockitoUsage"})
detect(IMethods mock)189         public void detect(IMethods mock) {
190             verifyZeroInteractions(mock);
191         }
192     }
193 
194     private static class OnVerifyNoMoreInteractions implements DetectsInvalidState {
195         @SuppressWarnings({"CheckReturnValue", "MockitoUsage"})
detect(IMethods mock)196         public void detect(IMethods mock) {
197             verifyNoMoreInteractions(mock);
198         }
199     }
200 
201     private static class OnDoAnswer implements DetectsInvalidState {
202         @SuppressWarnings({"CheckReturnValue", "MockitoUsage"})
detect(IMethods mock)203         public void detect(IMethods mock) {
204             doAnswer(null);
205         }
206     }
207 
208     private static class OnStub implements DetectsInvalidState {
209         @SuppressWarnings({"CheckReturnValue", "MockitoUsage"})
detect(IMethods mock)210         public void detect(IMethods mock) {
211             when(mock);
212         }
213     }
214 
215     private static class OnMethodCallOnMock implements DetectsInvalidState {
detect(IMethods mock)216         public void detect(IMethods mock) {
217             mock.simpleMethod();
218         }
219     }
220 
221     private static class OnMockCreation implements DetectsInvalidState {
222         @SuppressWarnings({"CheckReturnValue", "MockitoUsage"})
detect(IMethods mock)223         public void detect(IMethods mock) {
224             mock(IMethods.class);
225         }
226     }
227 
228     private static class OnSpyCreation implements DetectsInvalidState {
229         @SuppressWarnings({"CheckReturnValue", "MockitoUsage"})
detect(IMethods mock)230         public void detect(IMethods mock) {
231             spy(new Object());
232         }
233     }
234 
detectsAndCleansUp(DetectsInvalidState detector, Class<?> expected)235     private void detectsAndCleansUp(DetectsInvalidState detector, Class<?> expected) {
236         try {
237             detector.detect(mock);
238             fail("Should throw an exception");
239         } catch (Exception e) {
240             assertEquals(expected, e.getClass());
241         }
242         //Make sure state is cleaned up
243         new StateMaster().validate();
244     }
245 }
246