View Javadoc
1   package mockit;
2   
3   import static org.junit.jupiter.api.Assertions.assertFalse;
4   import static org.junit.jupiter.api.Assertions.assertTrue;
5   
6   import java.util.Observable;
7   import java.util.Observer;
8   import java.util.concurrent.Callable;
9   
10  import org.junit.jupiter.api.Test;
11  
12  /**
13   * The Class MultipleMockedTypesTest.
14   */
15  final class MultipleMockedTypesTest {
16  
17      /**
18       * The Class FirstDependency.
19       */
20      public static class FirstDependency {
21          /**
22           * Gets the value.
23           *
24           * @return the value
25           */
26          public int getValue() {
27              return 1;
28          }
29      }
30  
31      /**
32       * The Class SecondDependency.
33       */
34      public static class SecondDependency {
35  
36          /**
37           * Gets the value.
38           *
39           * @return the value
40           */
41          public int getValue() {
42              return 2;
43          }
44  
45          /**
46           * Gets the different value.
47           *
48           * @return the different value
49           */
50          public int getDifferentValue() {
51              return 3;
52          }
53      }
54  
55      /**
56       * The Class TestedUnit.
57       */
58      public static class TestedUnit {
59  
60          /**
61           * Validate.
62           *
63           * @param first
64           *            the first
65           *
66           * @return true, if successful
67           */
68          public boolean validate(FirstDependency first) {
69              SecondDependency second = new SecondDependency();
70              return first.getValue() + second.getValue() <= 0;
71          }
72  
73          /**
74           * Validate with different value.
75           *
76           * @param first
77           *            the first
78           *
79           * @return true, if successful
80           */
81          public boolean validateWithDifferentValue(FirstDependency first) {
82              SecondDependency second = new SecondDependency();
83              return first.getValue() + second.getDifferentValue() <= 0;
84          }
85  
86          /**
87           * Validate.
88           *
89           * @param first
90           *            the first
91           * @param second
92           *            the second
93           *
94           * @return true, if successful
95           */
96          public boolean validate(FirstDependency first, SecondDependency second) {
97              return first.getValue() + second.getValue() <= 0;
98          }
99  
100         /**
101          * Do something with internally created implementations.
102          */
103         static void doSomethingWithInternallyCreatedImplementations() {
104             new Observer() {
105                 @Override
106                 public void update(Observable o, Object arg) {
107                     throw new IllegalStateException();
108                 }
109             }.update(null, "event");
110 
111             new Callable<String>() {
112                 @Override
113                 public String call() {
114                     return "tested";
115                 }
116             }.call();
117         }
118     }
119 
120     /** The mock 1. */
121     @Mocked
122     FirstDependency mock1;
123 
124     /**
125      * Invocations on methods of different classes with different signatures.
126      *
127      * @param mock2
128      *            the mock 2
129      */
130     @Test
131     void invocationsOnMethodsOfDifferentClassesWithDifferentSignatures(@Mocked final SecondDependency mock2) {
132         new Expectations() {
133             {
134                 mock1.getValue();
135                 result = 15;
136                 mock2.getDifferentValue();
137                 result = -50;
138             }
139         };
140 
141         assertTrue(new TestedUnit().validateWithDifferentValue(mock1));
142     }
143 
144     /**
145      * Invocations on methods of different classes but same signature.
146      *
147      * @param mock2
148      *            the mock 2
149      */
150     @Test
151     void invocationsOnMethodsOfDifferentClassesButSameSignature(@Mocked final SecondDependency mock2) {
152         new Expectations() {
153             {
154                 mock1.getValue();
155                 result = 15;
156                 mock2.getValue();
157                 result = -50;
158             }
159         };
160 
161         assertTrue(new TestedUnit().validate(mock1));
162 
163         new VerificationsInOrder() {
164             {
165                 mock1.getValue();
166                 mock2.getValue();
167             }
168         };
169     }
170 
171     /**
172      * The Class SubDependencyThatInherits.
173      */
174     public static final class SubDependencyThatInherits extends SecondDependency {
175     }
176 
177     /**
178      * The Class SubDependencyThatOverrides.
179      */
180     public static final class SubDependencyThatOverrides extends SecondDependency {
181         @Override
182         public int getValue() {
183             return 1;
184         }
185     }
186 
187     /**
188      * Invocation on base type with replay on subtype that overrides the invoked method.
189      *
190      * @param mock2
191      *            the mock 2
192      */
193     @Test
194     void invocationOnBaseTypeWithReplayOnSubtypeThatOverridesTheInvokedMethod(@Mocked final SecondDependency mock2) {
195         new Expectations() {
196             {
197                 mock1.getValue();
198                 result = 15;
199             }
200         };
201 
202         // The executed method will be the override, which is not mocked.
203         assertFalse(new TestedUnit().validate(mock1, new SubDependencyThatOverrides()));
204 
205         new FullVerifications() {
206             {
207                 mock2.getValue();
208                 times = 0;
209             }
210         };
211     }
212 
213     /**
214      * Invocation on base type with capturing of subtype that inherits the invoked method.
215      *
216      * @param mock2
217      *            the mock 2
218      */
219     @Test
220     void invocationOnBaseTypeWithCapturingOfSubtypeThatInheritsTheInvokedMethod(
221             @Capturing final SecondDependency mock2) {
222         new Expectations() {
223             {
224                 mock1.getValue();
225                 result = 15;
226                 mock2.getValue();
227                 result = -50;
228             }
229         };
230 
231         assertTrue(new TestedUnit().validate(mock1, new SubDependencyThatInherits()));
232     }
233 
234     /**
235      * Invocation on base type with capturing of subtype that overrides the invoked method.
236      *
237      * @param mock2
238      *            the mock 2
239      */
240     @Test
241     void invocationOnBaseTypeWithCapturingOfSubtypeThatOverridesTheInvokedMethod(
242             @Capturing final SecondDependency mock2) {
243         new Expectations() {
244             {
245                 mock1.getValue();
246                 result = 15;
247                 mock2.getValue();
248                 result = -50;
249             }
250         };
251 
252         assertTrue(new TestedUnit().validate(mock1, new SubDependencyThatOverrides()));
253 
254         new VerificationsInOrder() {
255             {
256                 mock1.getValue();
257                 mock2.getValue();
258             }
259         };
260     }
261 
262     /**
263      * Invocations on captured implementations of interfaces.
264      *
265      * @param callable
266      *            the callable
267      * @param observer
268      *            the observer
269      *
270      * @throws Exception
271      *             the exception
272      */
273     @Test
274     void invocationsOnCapturedImplementationsOfInterfaces(@Capturing final Callable<String> callable,
275             @Capturing final Observer observer) throws Exception {
276         new Expectations() {
277             {
278                 observer.update(null, any);
279                 times = 1;
280             }
281         };
282 
283         TestedUnit.doSomethingWithInternallyCreatedImplementations();
284 
285         new Verifications() {
286             {
287                 callable.call();
288             }
289         };
290     }
291 }