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 org.junit.jupiter.api.Assertions.assertEquals;
9   import static org.junit.jupiter.api.Assertions.assertNotNull;
10  import static org.junit.jupiter.api.Assertions.assertSame;
11  
12  import mockit.integration.junit5.JMockitExtension;
13  
14  import org.junit.jupiter.api.Test;
15  import org.junit.jupiter.api.extension.ExtendWith;
16  
17  /**
18   * The Class TestedClassWithFullConstructorAndFieldDITest.
19   */
20  @ExtendWith(JMockitExtension.class)
21  class TestedClassWithFullConstructorAndFieldDITest {
22  
23      /**
24       * The Class TestedClass.
25       */
26      static class TestedClass {
27  
28          /** The value. */
29          String value;
30  
31          /** The dependency 1. */
32          DependencyWithFieldDIOnly dependency1;
33  
34          /** The dependency 2. */
35          DependencyWithConstructorDIOnly dependency2;
36      }
37  
38      /**
39       * The Class DependencyWithFieldDIOnly.
40       */
41      static class DependencyWithFieldDIOnly {
42          /** The value. */
43          String value;
44      }
45  
46      /**
47       * The Class DependencyWithConstructorDIOnly.
48       */
49      static class DependencyWithConstructorDIOnly {
50  
51          /** The value. */
52          final String value;
53  
54          /**
55           * Instantiates a new dependency with constructor DI only.
56           *
57           * @param value
58           *            the value
59           */
60          DependencyWithConstructorDIOnly(String value) {
61              this.value = value;
62          }
63      }
64  
65      /** The tested. */
66      @Tested(fullyInitialized = true)
67      TestedClass tested;
68  
69      /** The first. */
70      @Injectable
71      String first = "text";
72  
73      /**
74       * Verify each target field gets injected with first unused injectable whether through field or constructor
75       * injection.
76       */
77      @Test
78      void verifyEachTargetFieldGetsInjectedWithFirstUnusedInjectableWhetherThroughFieldOrConstructorInjection() {
79          assertEquals("text", tested.value);
80          assertEquals("text", tested.dependency1.value);
81          assertEquals("text", tested.dependency2.value);
82      }
83  
84      /**
85       * The Class ClassWithMultipleConstructors.
86       */
87      @SuppressWarnings("unused")
88      static class ClassWithMultipleConstructors {
89  
90          /** The value. */
91          final int value;
92  
93          /**
94           * Instantiates a new class with multiple constructors.
95           */
96          ClassWithMultipleConstructors() {
97              value = 1;
98          }
99  
100         /**
101          * Instantiates a new class with multiple constructors.
102          *
103          * @param value
104          *            the value
105          */
106         ClassWithMultipleConstructors(int value) {
107             throw new RuntimeException("Not to be called");
108         }
109     }
110 
111     /** The tested 2. */
112     @Tested(fullyInitialized = true)
113     ClassWithMultipleConstructors tested2;
114 
115     /**
116      * Verify initialization of class with multiple constructors.
117      */
118     @Test
119     void verifyInitializationOfClassWithMultipleConstructors() {
120         assertEquals(1, tested2.value);
121     }
122 
123     /**
124      * The Class ClassWithFieldToInject.
125      */
126     static class ClassWithFieldToInject {
127         /** The dependency. */
128         ClassWithMultipleConstructors dependency;
129     }
130 
131     /** The tested 3. */
132     @Tested(fullyInitialized = true)
133     ClassWithFieldToInject tested3;
134 
135     /**
136      * Verify initialization of class with field of another class having multiple constructors.
137      */
138     @Test
139     void verifyInitializationOfClassWithFieldOfAnotherClassHavingMultipleConstructors() {
140         assertNotNull(tested3.dependency);
141         assertEquals(1, tested3.dependency.value);
142     }
143 
144     /**
145      * The Class Dependency.
146      */
147     static final class Dependency {
148     }
149 
150     /**
151      * The Class AnotherClassWithMultipleConstructors.
152      */
153     @SuppressWarnings("unused")
154     static final class AnotherClassWithMultipleConstructors {
155 
156         /** The dep. */
157         final Dependency dep;
158 
159         /**
160          * Instantiates a new another class with multiple constructors.
161          */
162         AnotherClassWithMultipleConstructors() {
163             dep = new Dependency();
164         }
165 
166         /**
167          * Instantiates a new another class with multiple constructors.
168          *
169          * @param dep
170          *            the dep
171          */
172         AnotherClassWithMultipleConstructors(Dependency dep) {
173             this.dep = dep;
174         }
175     }
176 
177     /** The dep. */
178     @Tested
179     Dependency dep;
180 
181     /** The tested 4. */
182     @Tested(fullyInitialized = true)
183     AnotherClassWithMultipleConstructors tested4;
184 
185     /**
186      * Verify initialization of class with multiple constructors having tested field for parameter.
187      */
188     @Test
189     void verifyInitializationOfClassWithMultipleConstructorsHavingTestedFieldForParameter() {
190         assertSame(dep, tested4.dep);
191     }
192 
193     /**
194      * The Class ClassWithFieldDI.
195      */
196     static class ClassWithFieldDI {
197         /** The dep. */
198         Dependency dep;
199     }
200 
201     /**
202      * The Class ClassWithConstructorDI.
203      */
204     static class ClassWithConstructorDI {
205 
206         /** The dependency. */
207         ClassWithFieldDI dependency;
208 
209         /**
210          * Instantiates a new class with constructor DI.
211          *
212          * @param dependency
213          *            the dependency
214          */
215         ClassWithConstructorDI(ClassWithFieldDI dependency) {
216             this.dependency = dependency;
217         }
218     }
219 
220     /** The tested 5. */
221     @Tested(fullyInitialized = true)
222     ClassWithConstructorDI tested5;
223 
224     /**
225      * Initialize class with constructor injected dependency having another dependency injected into field.
226      */
227     @Test
228     void initializeClassWithConstructorInjectedDependencyHavingAnotherDependencyInjectedIntoField() {
229         assertNotNull(tested5.dependency);
230         assertNotNull(tested5.dependency.dep);
231     }
232 }