View Javadoc
1   package mockit;
2   
3   import static java.lang.annotation.ElementType.PARAMETER;
4   import static java.lang.annotation.RetentionPolicy.RUNTIME;
5   
6   import static org.junit.jupiter.api.Assertions.assertEquals;
7   import static org.junit.jupiter.api.Assertions.assertNotNull;
8   import static org.junit.jupiter.api.Assertions.assertNull;
9   import static org.junit.jupiter.api.Assertions.assertSame;
10  
11  import java.lang.annotation.Retention;
12  import java.lang.annotation.Target;
13  
14  import javax.inject.Inject;
15  
16  import org.junit.jupiter.api.Test;
17  
18  /**
19   * The Class TestedParametersTest.
20   */
21  final class TestedParametersTest {
22  
23      /**
24       * The Class TestedClass.
25       */
26      static class TestedClass {
27  
28          /** The i. */
29          final int i;
30  
31          /** The collaborator. */
32          final Collaborator collaborator;
33  
34          /** The dependency. */
35          Dependency dependency;
36  
37          /**
38           * Instantiates a new tested class.
39           */
40          TestedClass() {
41              i = -1;
42              collaborator = null;
43          }
44  
45          /**
46           * Instantiates a new tested class.
47           *
48           * @param i
49           *            the i
50           * @param collaborator
51           *            the collaborator
52           */
53          TestedClass(int i, Collaborator collaborator) {
54              this.i = i;
55              this.collaborator = collaborator;
56          }
57      }
58  
59      /**
60       * The Class Dependency.
61       */
62      static class Dependency {
63      }
64  
65      /**
66       * The Class Collaborator.
67       */
68      static final class Collaborator {
69      }
70  
71      /**
72       * Creates the tested object for test method parameter.
73       *
74       * @param dep
75       *            the dep
76       */
77      @Test
78      void createTestedObjectForTestMethodParameter(@Tested Dependency dep) {
79          assertNotNull(dep);
80      }
81  
82      /** The tested 1. */
83      @Tested
84      TestedClass tested1;
85  
86      /** The tested 2. */
87      @Tested(fullyInitialized = true)
88      TestedClass tested2;
89  
90      /**
91       * Inject tested object from test method parameter into fully initialized tested object.
92       *
93       * @param dep
94       *            the dep
95       */
96      @Test
97      void injectTestedObjectFromTestMethodParameterIntoFullyInitializedTestedObject(@Tested Dependency dep) {
98          assertEquals(-1, tested2.i);
99          assertNull(tested2.collaborator);
100         assertSame(dep, tested2.dependency);
101     }
102 
103     /**
104      * Inject tested parameters into tested fields using constructor.
105      *
106      * @param i
107      *            the i
108      * @param collaborator
109      *            the collaborator
110      */
111     @Test
112     void injectTestedParametersIntoTestedFieldsUsingConstructor(@Tested("123") int i,
113             @Tested Collaborator collaborator) {
114         assertEquals(123, i);
115         assertNotNull(collaborator);
116 
117         assertEquals(123, tested1.i);
118         assertSame(collaborator, tested1.collaborator);
119         assertNull(tested1.dependency);
120 
121         assertEquals(123, tested2.i);
122         assertSame(collaborator, tested2.collaborator);
123         assertNotNull(tested2.dependency);
124     }
125 
126     /**
127      * The Class TestedClass2.
128      */
129     static class TestedClass2 {
130         /** The text. */
131         CharSequence text;
132         /** The n. */
133         Number n;
134         /** The cmp. */
135         Comparable<Float> cmp;
136     }
137 
138     /**
139      * Inject tested parameters into tested fields of supertypes.
140      *
141      * @param s
142      *            the s
143      * @param n
144      *            the n
145      * @param cmp
146      *            the cmp
147      * @param tested
148      *            the tested
149      */
150     @Test
151     void injectTestedParametersIntoTestedFieldsOfSupertypes(@Tested("test") String s, @Tested("123") Integer n,
152             @Tested("5.2") Float cmp, @Tested(fullyInitialized = true) TestedClass2 tested) {
153         assertEquals("test", tested.text);
154         assertEquals(123, tested.n.intValue());
155         assertEquals(5.2F, tested.cmp);
156     }
157 
158     /**
159      * The Class TestedClass3.
160      */
161     static class TestedClass3 {
162         /** The text. */
163         String text;
164         /** The number. */
165         Number number;
166     }
167 
168     /**
169      * Inject tested parameters with values into fields of regular tested object.
170      *
171      * @param s
172      *            the s
173      * @param n
174      *            the n
175      * @param tested
176      *            the tested
177      */
178     @Test
179     void injectTestedParametersWithValuesIntoFieldsOfRegularTestedObject(@Tested("test") String s,
180             @Tested("123") Integer n, @Tested TestedClass3 tested) {
181         assertEquals("test", tested.text);
182         assertEquals(123, tested.number);
183     }
184 
185     /**
186      * The Class TestedClass4.
187      */
188     static class TestedClass4 {
189 
190         /** The text. */
191         final String text;
192 
193         /** The number. */
194         final Number number;
195 
196         /**
197          * Instantiates a new tested class 4.
198          *
199          * @param text
200          *            the text
201          * @param number
202          *            the number
203          */
204         TestedClass4(String text, Number number) {
205             this.text = text;
206             this.number = number;
207         }
208     }
209 
210     /**
211      * Inject tested parameter with value into regular tested object through constructor parameter.
212      *
213      * @param text
214      *            the text
215      * @param number
216      *            the number
217      * @param tested
218      *            the tested
219      */
220     @Test
221     void injectTestedParameterWithValueIntoRegularTestedObjectThroughConstructorParameter(@Tested("test") String text,
222             @Tested("1.23") Double number, @Tested TestedClass4 tested) {
223         assertEquals("test", tested.text);
224         assertEquals(1.23, tested.number);
225     }
226 
227     /**
228      * The Class AnotherDependency.
229      */
230     static class AnotherDependency {
231     }
232 
233     /**
234      * The Class TestedClassWithDIAnnotatedField.
235      */
236     static class TestedClassWithDIAnnotatedField {
237         /** The dep. */
238         @Inject
239         AnotherDependency dep;
240     }
241 
242     /** The another dep. */
243     @Injectable
244     AnotherDependency anotherDep;
245 
246     /**
247      * Inject injectable field into tested parameter.
248      *
249      * @param tested
250      *            the tested
251      */
252     @Test
253     void injectInjectableFieldIntoTestedParameter(@Tested TestedClassWithDIAnnotatedField tested) {
254         assertSame(anotherDep, tested.dep);
255     }
256 
257     /**
258      * The Interface InjectedDependency.
259      */
260     @Target(PARAMETER)
261     @Retention(RUNTIME)
262     @Tested
263     public @interface InjectedDependency {
264     }
265 
266     /**
267      * Inject parameter using tested as meta annotation.
268      *
269      * @param col
270      *            the col
271      */
272     @Test
273     void injectParameterUsingTestedAsMetaAnnotation(@InjectedDependency Collaborator col) {
274         assertNotNull(col);
275     }
276 }