View Javadoc
1   package mockit;
2   
3   import static java.util.Arrays.asList;
4   
5   import static org.junit.jupiter.api.Assertions.assertEquals;
6   import static org.junit.jupiter.api.Assertions.assertNotNull;
7   import static org.junit.jupiter.api.Assertions.assertNull;
8   import static org.junit.jupiter.api.Assertions.assertSame;
9   
10  import java.io.File;
11  import java.util.List;
12  import java.util.concurrent.Callable;
13  
14  import javax.inject.Inject;
15  import javax.naming.InitialContext;
16  import javax.naming.NamingException;
17  import javax.sql.DataSource;
18  
19  import org.junit.Before;
20  import org.junit.Test;
21  
22  /**
23   * The Class TestedClassWithConstructorDI4Test.
24   */
25  public final class TestedClassWithConstructorDI4Test {
26  
27      /**
28       * The Class GenericClass.
29       *
30       * @param <T>
31       *            the generic type
32       */
33      static class GenericClass<T> {
34          /**
35           * Do something.
36           *
37           * @return the t
38           */
39          T doSomething() {
40              return null;
41          }
42      }
43  
44      /**
45       * The Class TestedClass.
46       */
47      @SuppressWarnings("FieldMayBeFinal")
48      public static final class TestedClass {
49  
50          /** The go. */
51          final GenericClass<String> go;
52  
53          /** The values. */
54          final List<Integer> values;
55  
56          /** The action 1. */
57          final Callable<Number> action1;
58  
59          /** The action 2. */
60          private Callable<Number> action2;
61  
62          /** The action 3. */
63          private Callable<Number> action3;
64  
65          /** The database. */
66          private DataSource database;
67  
68          /**
69           * Instantiates a new tested class.
70           *
71           * @param go
72           *            the go
73           * @param values
74           *            the values
75           * @param actions
76           *            the actions
77           */
78          @SuppressWarnings("unchecked")
79          public TestedClass(GenericClass<String> go, List<Integer> values, Callable<Number>... actions) {
80              this.go = go;
81              this.values = values;
82              action1 = actions[0];
83              if (actions.length > 1) {
84                  action2 = actions[1];
85              }
86              if (actions.length > 2) {
87                  action3 = actions[2];
88              }
89  
90              try {
91                  // noinspection JNDIResourceOpenedButNotSafelyClosed
92                  InitialContext context = new InitialContext();
93                  database = (DataSource) context.lookup("testDB");
94                  context.close();
95              } catch (NamingException e) {
96                  throw new RuntimeException(e);
97              }
98          }
99      }
100 
101     /** The tested. */
102     @Tested
103     TestedClass tested;
104 
105     /** The action 1. */
106     @Injectable
107     Callable<Number> action1;
108 
109     /** The mock GO. */
110     @Injectable
111     final GenericClass<String> mockGO = new GenericClass<>(); // still mocked
112 
113     @Injectable
114     final List<Integer> numbers = asList(1, 2, 3); // not mocked when interface
115 
116     /** The jndi context. */
117     @Mocked
118     InitialContext jndiContext;
119 
120     /** The test DB. */
121     @Mocked
122     DataSource testDB;
123 
124     /**
125      * Record common expectations.
126      *
127      * @throws Exception
128      *             the exception
129      */
130     @Before
131     public void recordCommonExpectations() throws Exception {
132         new Expectations() {
133             {
134                 mockGO.doSomething();
135                 result = "test";
136                 minTimes = 0;
137             }
138         };
139         new Expectations() {
140             {
141                 jndiContext.lookup("testDB");
142                 result = testDB;
143             }
144         };
145     }
146 
147     /**
148      * Exercise tested object with values injected from mock fields.
149      */
150     @Test
151     public void exerciseTestedObjectWithValuesInjectedFromMockFields() {
152         assertNotNull(tested.go);
153         assertEquals(asList(1, 2, 3), tested.values);
154         assertSame(action1, tested.action1);
155         assertEquals("test", mockGO.doSomething());
156         assertNull(new GenericClass<String>().doSomething());
157     }
158 
159     /**
160      * Exercise tested object with values injected from mock parameters.
161      *
162      * @param action2
163      *            the action 2
164      * @param action3
165      *            the action 3
166      */
167     @Test
168     public void exerciseTestedObjectWithValuesInjectedFromMockParameters(@Injectable Callable<Number> action2,
169             @Injectable Callable<Number> action3) {
170         assertNotNull(tested.go);
171         assertEquals(asList(1, 2, 3), tested.values);
172         assertSame(action1, tested.action1);
173         assertSame(action2, tested.action2);
174         assertSame(action3, tested.action3);
175         assertEquals("test", mockGO.doSomething());
176         assertNull(new GenericClass().doSomething());
177     }
178 
179     /**
180      * Use mocked JRE classes during tested object creation.
181      *
182      * @param fileMock
183      *            the file mock
184      */
185     @Test
186     public void useMockedJREClassesDuringTestedObjectCreation(@Mocked File fileMock) {
187         assertNotNull(tested.database);
188         mockGO.doSomething();
189     }
190 
191     /**
192      * The Class TestedClass3.
193      */
194     static class TestedClass3 {
195 
196         /** The text. */
197         final String text;
198 
199         /** The dependency. */
200         final Runnable dependency;
201 
202         /** The other dep. */
203         @Inject
204         GenericClass<Integer> otherDep;
205 
206         /**
207          * Instantiates a new tested class 3.
208          *
209          * @param text
210          *            the text
211          * @param dependency
212          *            the dependency
213          */
214         TestedClass3(String text, Runnable dependency) {
215             this.text = text;
216             this.dependency = dependency;
217         }
218     }
219 
220     /** The tested 7. */
221     @Tested
222     TestedClass3 tested7;
223 
224     /** The text. */
225     @Injectable
226     final String text = null;
227 
228     /** The dependency. */
229     @Injectable
230     final Runnable dependency = null;
231 
232     /** The other dep. */
233     @Injectable
234     final GenericClass<Integer> otherDep = null;
235 
236     /**
237      * Inject nulls through constructor parameters and into required field.
238      */
239     @Test
240     public void injectNullsThroughConstructorParametersAndIntoRequiredField() {
241         assertNull(tested7.text);
242         assertNull(tested7.dependency);
243         assertNull(tested7.otherDep);
244     }
245 }