View Javadoc
1   package mockit;
2   
3   import static org.junit.jupiter.api.Assertions.assertEquals;
4   import static org.junit.jupiter.api.Assertions.assertNotSame;
5   import static org.junit.jupiter.api.Assertions.assertSame;
6   import static org.junit.jupiter.api.Assertions.fail;
7   
8   import org.junit.Rule;
9   import org.junit.Test;
10  import org.junit.rules.ExpectedException;
11  
12  /**
13   * The Class MisusedMockingAPITest.
14   */
15  public final class MisusedMockingAPITest {
16  
17      /** The thrown. */
18      @Rule
19      public final ExpectedException thrown = ExpectedException.none();
20  
21      /**
22       * The Class Blah.
23       */
24      static class Blah {
25  
26          /** The name. */
27          @SuppressWarnings("RedundantStringConstructorCall")
28          final String name = "Blah";
29  
30          /**
31           * Value.
32           *
33           * @return the int
34           */
35          int value() {
36              return -1;
37          }
38  
39          /**
40           * Sets the value.
41           *
42           * @param value
43           *            the new value
44           */
45          void setValue(@SuppressWarnings("unused") int value) {
46          }
47  
48          /**
49           * Do something.
50           *
51           * @param b
52           *            the b
53           *
54           * @return the string
55           */
56          String doSomething(@SuppressWarnings("unused") boolean b) {
57              return "";
58          }
59  
60          /**
61           * Gets the something else.
62           *
63           * @return the something else
64           */
65          Runnable getSomethingElse() {
66              return null;
67          }
68      }
69  
70      /** The mock. */
71      @Mocked
72      Blah mock;
73  
74      // Arrange-Act-Assert non-conformance
75      // //////////////////////////////////////////////////////////////////////////////////////////////////
76  
77      /**
78       * Record expectation after invoking same method in replay phase.
79       */
80      @Test
81      public void recordExpectationAfterInvokingSameMethodInReplayPhase() {
82          assertEquals(0, mock.value());
83  
84          new Expectations() {
85              {
86                  mock.value();
87                  result = 1;
88              }
89          };
90  
91          assertEquals(1, mock.value());
92      }
93  
94      // Duplicate/pointless recordings
95      // //////////////////////////////////////////////////////////////////////////////////////////////////////
96  
97      /**
98       * Record duplicate invocation with no arguments.
99       */
100     @Test
101     public void recordDuplicateInvocationWithNoArguments() {
102         new Expectations() {
103             {
104                 mock.value();
105                 result = 1;
106                 mock.value();
107                 result = 2; // second recording overrides the first
108             }
109         };
110 
111         assertEquals(2, mock.value());
112         assertEquals(2, mock.value());
113     }
114 
115     /**
116      * Record duplicate invocation with argument matcher.
117      */
118     @Test
119     public void recordDuplicateInvocationWithArgumentMatcher() {
120         new Expectations() {
121             {
122                 mock.setValue(anyInt);
123                 result = new UnknownError();
124                 mock.setValue(anyInt); // overrides the previous one
125             }
126         };
127 
128         mock.setValue(3);
129     }
130 
131     /**
132      * Record duplicate invocation in separate expectation blocks.
133      */
134     @Test
135     public void recordDuplicateInvocationInSeparateExpectationBlocks() {
136         new Expectations() {
137             {
138                 mock.value();
139                 result = 1;
140             }
141         };
142         new Expectations() {
143             {
144                 mock.value();
145                 result = 2;
146             }
147         }; // overrides the previous expectation
148 
149         assertEquals(2, mock.value());
150     }
151 
152     /**
153      * Record mock instance for constructor expectation.
154      */
155     @Test
156     public void recordMockInstanceForConstructorExpectation() {
157         thrown.expect(IllegalArgumentException.class);
158         thrown.expectMessage("Invalid assignment");
159         thrown.expectMessage("result ");
160         thrown.expectMessage(" constructor");
161 
162         new Expectations() {
163             {
164                 new Blah();
165                 result = mock;
166             }
167         };
168 
169         new Blah();
170     }
171 
172     // Order-related recordings
173     // ////////////////////////////////////////////////////////////////////////////////////////////////////////////
174 
175     /**
176      * Record unordered instantiation of class mocked twice.
177      *
178      * @param mock2
179      *            the mock 2
180      */
181     @Test
182     public void recordUnorderedInstantiationOfClassMockedTwice(@Mocked final Blah mock2) {
183         new Expectations() {
184             {
185                 new Blah();
186                 times = 1;
187                 mock.value();
188                 result = 123;
189                 mock2.value();
190                 result = 45;
191             }
192         };
193 
194         assertEquals(45, mock2.value());
195         assertEquals(123, mock.value());
196         new Blah();
197     }
198 
199     /**
200      * Verify ordered instantiation of class mocked twice.
201      *
202      * @param mock2
203      *            the mock 2
204      */
205     @Test
206     public void verifyOrderedInstantiationOfClassMockedTwice(@Mocked final Blah mock2) {
207         new Blah();
208         mock2.doSomething(true);
209 
210         new VerificationsInOrder() {
211             {
212                 new Blah();
213                 mock2.doSomething(anyBoolean);
214             }
215         };
216     }
217 
218     /**
219      * Verify unordered instantiation of class mocked twice.
220      *
221      * @param mock2
222      *            the mock 2
223      */
224     @Test
225     public void verifyUnorderedInstantiationOfClassMockedTwice(@Mocked final Blah mock2) {
226         mock.doSomething(false);
227         mock2.doSomething(true);
228         new Blah();
229 
230         new Verifications() {
231             {
232                 mock2.doSomething(true);
233                 new Blah();
234                 mock.doSomething(false);
235             }
236         };
237     }
238 
239     // Cascading
240     // ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
241 
242     /**
243      * Ambiguous cascading when multiple valid candidates are available.
244      *
245      * @param r1
246      *            the r 1
247      * @param r2
248      *            the r 2
249      */
250     @Test
251     public void ambiguousCascadingWhenMultipleValidCandidatesAreAvailable(@Injectable Runnable r1,
252             @Injectable Runnable r2) {
253         Runnable cascaded = mock.getSomethingElse(); // which one to return: r1 or r2?
254 
255         assertSame(r2, cascaded); // currently, last mock to be declared wins
256     }
257 
258     // @Tested/@Injectable usage
259     // ///////////////////////////////////////////////////////////////////////////////////////////////////////////
260 
261     /**
262      * The Class TestedClass.
263      */
264     static class TestedClass {
265         /** The action. */
266         Runnable action;
267     }
268 
269     /** The mock action. */
270     @Injectable
271     static Runnable mockAction = new Runnable() {
272         @Override
273         public void run() {
274         }
275     };
276 
277     /** The tested. */
278     @Tested
279     TestedClass tested;
280 
281     /**
282      * Check static injectable is not used.
283      */
284     @Test
285     public void checkStaticInjectableIsNotUsed() {
286         assertNotSame(mockAction, tested.action);
287     }
288 
289     // Other cases
290     // /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
291 
292     /**
293      * The Class CustomException.
294      */
295     static final class CustomException extends Exception {
296 
297         private static final long serialVersionUID = 1L;
298     }
299 
300     /**
301      * Attempting to mock all instances of exception subclass.
302      *
303      * @param anyCustomException
304      *            the any custom exception
305      */
306     @Test(expected = IllegalArgumentException.class)
307     public void attemptingToMockAllInstancesOfExceptionSubclass(@Mocked CustomException anyCustomException) {
308         fail("Shouldn't get here");
309     }
310 }