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