View Javadoc
1   package mockit;
2   
3   import static org.junit.jupiter.api.Assertions.assertEquals;
4   import static org.junit.jupiter.api.Assertions.assertFalse;
5   import static org.junit.jupiter.api.Assertions.assertNotNull;
6   import static org.junit.jupiter.api.Assertions.assertNull;
7   import static org.junit.jupiter.api.Assertions.assertTrue;
8   
9   import java.beans.BeanInfo;
10  import java.beans.Introspector;
11  
12  import org.junit.jupiter.api.Test;
13  
14  /**
15   * The Class ExpectationsUsingMockedTest.
16   */
17  final class ExpectationsUsingMockedTest {
18  
19      /**
20       * The Interface Dependency.
21       */
22      public interface Dependency {
23          /**
24           * Do something.
25           *
26           * @param b
27           *            the b
28           *
29           * @return the string
30           */
31          String doSomething(boolean b);
32      }
33  
34      /**
35       * The Class AbstractBase.
36       */
37      public abstract static class AbstractBase {
38  
39          /**
40           * Adds the.
41           *
42           * @param i
43           *            the i
44           *
45           * @return true, if successful
46           */
47          protected abstract boolean add(Integer i);
48  
49          /**
50           * Do something.
51           *
52           * @return the int
53           */
54          final int doSomething() {
55              return -1;
56          }
57      }
58  
59      /** The base. */
60      @Mocked
61      AbstractBase base;
62  
63      /**
64       * Multiple mock parameters of same mocked type.
65       *
66       * @param dependency1
67       *            the dependency 1
68       * @param dependency2
69       *            the dependency 2
70       */
71      @Test
72      void multipleMockParametersOfSameMockedType(@Mocked final Dependency dependency1,
73              @Mocked final Dependency dependency2) {
74          new Expectations() {
75              {
76                  dependency1.doSomething(true);
77                  result = "1";
78                  dependency2.doSomething(false);
79                  result = "2";
80              }
81          };
82  
83          assertEquals("1", dependency1.doSomething(true));
84          assertNull(dependency1.doSomething(false));
85          assertEquals("2", dependency2.doSomething(false));
86          assertNull(dependency2.doSomething(true));
87      }
88  
89      /**
90       * Mock field for abstract class.
91       */
92      @Test
93      void mockFieldForAbstractClass() {
94          new Expectations() {
95              {
96                  base.add(1);
97                  result = true;
98              }
99          };
100 
101         assertFalse(base.add(0));
102         assertTrue(base.add(1));
103         assertFalse(base.add(2));
104     }
105 
106     static class ClassWithStaticInitializer {
107         static boolean initialized = true;
108 
109         static int initialized() {
110             return initialized ? 1 : -1;
111         }
112     }
113 
114     @Test
115     public void stubOutStaticInitializersWhenSpecified(
116             @Mocked(stubOutClassInitialization = true) ClassWithStaticInitializer unused) {
117         assertEquals(0, ClassWithStaticInitializer.initialized());
118         assertFalse(ClassWithStaticInitializer.initialized);
119     }
120 
121     /**
122      * The Class ClassWithStaticInitializer2.
123      */
124     static class ClassWithStaticInitializer2 {
125 
126         /** The initialized. */
127         static boolean initialized = true;
128 
129         /**
130          * Initialized.
131          *
132          * @return the int
133          */
134         static int initialized() {
135             return initialized ? 1 : -1;
136         }
137     }
138 
139     /**
140      * Do not stub out static initializers by default.
141      *
142      * @param unused
143      *            the unused
144      */
145     @Test
146     void doNotStubOutStaticInitializersByDefault(@Mocked ClassWithStaticInitializer2 unused) {
147         assertEquals(0, ClassWithStaticInitializer2.initialized());
148         assertTrue(ClassWithStaticInitializer2.initialized);
149     }
150 
151     /**
152      * The Class AnotherClassWithStaticInitializer.
153      */
154     static class AnotherClassWithStaticInitializer {
155 
156         /** The initialized. */
157         static boolean initialized = true;
158 
159         /**
160          * Initialized.
161          *
162          * @return the int
163          */
164         static int initialized() {
165             return initialized ? 1 : -1;
166         }
167     }
168 
169     /**
170      * Mock everything without stubbing static initializers.
171      *
172      * @param unused
173      *            the unused
174      */
175     @Test
176     void mockEverythingWithoutStubbingStaticInitializers(@Mocked AnotherClassWithStaticInitializer unused) {
177         assertEquals(0, AnotherClassWithStaticInitializer.initialized());
178         assertTrue(AnotherClassWithStaticInitializer.initialized);
179     }
180 
181     static class AnotherClassWithStaticInitializer2 {
182         static boolean initialized = true;
183 
184         static int initialized() {
185             return initialized ? 1 : -1;
186         }
187     }
188 
189     @Test
190     @SuppressWarnings("DefaultAnnotationParam")
191     public void avoidStubbingStaticInitializersThroughSpecificAnnotationAttribute(
192             @Mocked(stubOutClassInitialization = false) AnotherClassWithStaticInitializer2 unused) {
193         assertEquals(0, AnotherClassWithStaticInitializer2.initialized());
194         assertTrue(AnotherClassWithStaticInitializer2.initialized);
195     }
196 
197     /**
198      * The Class InnerClass.
199      */
200     static class InnerClass {
201         /**
202          * Gets the value.
203          *
204          * @return the value
205          */
206         int getValue() {
207             return -1;
208         }
209     }
210 
211     /**
212      * Mock inner class.
213      *
214      * @param innerMock
215      *            the inner mock
216      */
217     @Test
218     void mockInnerClass(@Mocked final InnerClass innerMock) {
219         assertEquals(0, innerMock.getValue());
220 
221         new Expectations() {
222             {
223                 innerMock.getValue();
224                 result = 123;
225                 times = 1;
226             }
227         };
228 
229         assertEquals(123, new InnerClass().getValue());
230     }
231 
232     /**
233      * The Class SubClass.
234      */
235     static final class SubClass extends AbstractBase {
236         @Override
237         protected boolean add(Integer i) {
238             return false;
239         }
240     }
241 
242     /**
243      * Record method from abstract base class and replay on subclass.
244      */
245     @Test
246     void recordMethodFromAbstractBaseClassAndReplayOnSubclass() {
247         new Expectations() {
248             {
249                 base.doSomething();
250                 result = 1;
251             }
252         };
253 
254         assertEquals(1, base.doSomething());
255         assertEquals(-1, new SubClass().doSomething());
256     }
257 
258     /**
259      * The Interface BusinessInterface.
260      */
261     public interface BusinessInterface {
262     }
263 
264     /**
265      * Gets the bean info from mocked interface.
266      *
267      * @param mock
268      *            the mock
269      *
270      * @throws Exception
271      *             the exception
272      */
273     @Test
274     void beanInfoFromMockedInterface(@Mocked BusinessInterface mock) throws Exception {
275         Class<? extends BusinessInterface> mockClass = mock.getClass();
276 
277         BeanInfo info = Introspector.getBeanInfo(mockClass);
278 
279         assertNotNull(info);
280     }
281 
282     /**
283      * The Class GenericBase.
284      *
285      * @param <B>
286      *            the generic type
287      */
288     static class GenericBase<B extends Runnable> {
289         /**
290          * Base.
291          *
292          * @return the b
293          */
294         public B base() {
295             return null;
296         }
297     }
298 
299     /**
300      * The Class GenericSubclass.
301      *
302      * @param <S>
303      *            the generic type
304      */
305     public static final class GenericSubclass<S extends Runnable> extends GenericBase<S> {
306         /* bridge method here */ }
307 
308     /**
309      * Record expectation on base method having A synthetic bridge method in subclass.
310      *
311      * @param mock
312      *            the mock
313      */
314     @Test
315     void recordExpectationOnBaseMethodHavingASyntheticBridgeMethodInSubclass(@Mocked final GenericSubclass<?> mock) {
316         new Expectations() {
317             {
318                 mock.base();
319                 result = null;
320             }
321         };
322 
323         assertNull(mock.base());
324     }
325 }