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