View Javadoc
1   package mockit;
2   
3   import static org.junit.jupiter.api.Assertions.assertEquals;
4   import static org.junit.jupiter.api.Assertions.assertTrue;
5   
6   import java.util.Observable;
7   
8   import javax.annotation.PostConstruct;
9   import javax.annotation.PreDestroy;
10  
11  import org.junit.jupiter.api.AfterEach;
12  import org.junit.jupiter.api.BeforeEach;
13  import org.junit.jupiter.api.Test;
14  
15  /**
16   * The Class TestedClassWithConstructorDI1Test.
17   */
18  final class TestedClassWithConstructorDI1Test {
19  
20      /**
21       * The Class BaseTestedClass.
22       */
23      public static class BaseTestedClass {
24  
25          /** The base counter. */
26          static int baseCounter;
27  
28          /**
29           * Initialize base.
30           */
31          @PostConstruct
32          void initializeBase() {
33              baseCounter++;
34          }
35  
36          /**
37           * Destroy base.
38           */
39          @PreDestroy
40          void destroyBase() {
41              baseCounter++;
42          }
43      }
44  
45      /**
46       * The Class TestedClass.
47       */
48      public static final class TestedClass extends BaseTestedClass {
49  
50          /** The counter. */
51          static int counter;
52  
53          /** The dependency. */
54          private final Dependency dependency;
55  
56          /** The runnable. */
57          private final Runnable runnable;
58  
59          /** The observable. */
60          private final Observable observable;
61  
62          /**
63           * Instantiates a new tested class.
64           *
65           * @param dependency
66           *            the dependency
67           */
68          public TestedClass(Dependency dependency) {
69              this(dependency, null, null);
70          }
71  
72          /**
73           * Instantiates a new tested class.
74           *
75           * @param dependency
76           *            the dependency
77           * @param runnable
78           *            the runnable
79           */
80          public TestedClass(Dependency dependency, Runnable runnable) {
81              this(dependency, runnable, null);
82          }
83  
84          /**
85           * Instantiates a new tested class.
86           *
87           * @param dependency
88           *            the dependency
89           * @param observable
90           *            the observable
91           */
92          public TestedClass(Dependency dependency, Observable observable) {
93              this(dependency, null, observable);
94          }
95  
96          /**
97           * Instantiates a new tested class.
98           *
99           * @param dependency
100          *            the dependency
101          * @param runnable
102          *            the runnable
103          * @param observable
104          *            the observable
105          */
106         public TestedClass(Dependency dependency, Runnable runnable, Observable observable) {
107             this.dependency = dependency;
108             this.runnable = runnable;
109             this.observable = observable;
110         }
111 
112         /**
113          * Do some operation.
114          *
115          * @return true, if successful
116          */
117         public boolean doSomeOperation() {
118             if (runnable != null) {
119                 runnable.run();
120             }
121 
122             boolean b = dependency.doSomething() > 0;
123 
124             if (b && observable != null) {
125                 observable.notifyObservers();
126             }
127 
128             return b;
129         }
130 
131         /**
132          * Initialize.
133          */
134         @PostConstruct
135         void initialize() {
136             counter++;
137         }
138 
139         /**
140          * Destroy.
141          */
142         @PreDestroy
143         void destroy() {
144             counter++;
145         }
146     }
147 
148     /**
149      * The Class Dependency.
150      */
151     static class Dependency {
152         /**
153          * Do something.
154          *
155          * @return the int
156          */
157         int doSomething() {
158             return -1;
159         }
160     }
161 
162     /** The tested. */
163     @Tested
164     TestedClass tested;
165 
166     /** The mock. */
167     @Injectable
168     Dependency mock;
169 
170     /**
171      * Exercise tested object with single dependency injected through constructor.
172      */
173     @Test
174     void exerciseTestedObjectWithSingleDependencyInjectedThroughConstructor() {
175         assertTestedObjectWasInitialized();
176 
177         new Expectations() {
178             {
179                 mock.doSomething();
180                 result = 23;
181             }
182         };
183 
184         assertTrue(tested.doSomeOperation());
185     }
186 
187     /**
188      * Exercise tested object with two dependencies injected through constructor.
189      *
190      * @param mock2
191      *            the mock 2
192      */
193     @Test
194     void exerciseTestedObjectWithTwoDependenciesInjectedThroughConstructor(@Injectable final Runnable mock2) {
195         assertTestedObjectWasInitialized();
196 
197         new Expectations() {
198             {
199                 mock.doSomething();
200                 result = 23;
201             }
202         };
203 
204         assertTrue(tested.doSomeOperation());
205 
206         new Verifications() {
207             {
208                 mock2.run();
209             }
210         };
211     }
212 
213     /**
214      * Exercise tested object with two other dependencies injected through constructor.
215      *
216      * @param obs
217      *            the obs
218      */
219     @Test
220     void exerciseTestedObjectWithTwoOtherDependenciesInjectedThroughConstructor(@Injectable final Observable obs) {
221         assertTestedObjectWasInitialized();
222 
223         new Expectations() {
224             {
225                 mock.doSomething();
226                 result = 123;
227             }
228         };
229 
230         assertTrue(tested.doSomeOperation());
231 
232         new FullVerifications() {
233             {
234                 obs.notifyObservers();
235             }
236         };
237     }
238 
239     /**
240      * Exercise tested object with all dependencies injected through constructor.
241      *
242      * @param mock2
243      *            the mock 2
244      * @param mock3
245      *            the mock 3
246      */
247     @Test
248     void exerciseTestedObjectWithAllDependenciesInjectedThroughConstructor(@Injectable final Runnable mock2,
249             @Injectable final Observable mock3) {
250         assertTestedObjectWasInitialized();
251 
252         new Expectations() {
253             {
254                 mock.doSomething();
255                 result = 123;
256             }
257         };
258 
259         assertTrue(tested.doSomeOperation());
260 
261         new VerificationsInOrder() {
262             {
263                 mock2.run();
264                 mock3.notifyObservers();
265             }
266         };
267     }
268 
269     /**
270      * Reset counter.
271      */
272     @BeforeEach
273     void resetCounter() {
274         BaseTestedClass.baseCounter = 0;
275         TestedClass.counter = 0;
276     }
277 
278     /**
279      * Assert tested object was initialized.
280      */
281     void assertTestedObjectWasInitialized() {
282         assertEquals(1, BaseTestedClass.baseCounter);
283         assertEquals(1, TestedClass.counter);
284     }
285 
286     /**
287      * Verify tested object after every test.
288      */
289     @AfterEach
290     void verifyTestedObjectAfterEveryTest() {
291         assertEquals(2, BaseTestedClass.baseCounter);
292         assertEquals(2, TestedClass.counter);
293     }
294 }