View Javadoc
1   package mockit;
2   
3   import static org.junit.jupiter.api.Assertions.assertEquals;
4   import static org.junit.jupiter.api.Assertions.assertNull;
5   import static org.junit.jupiter.api.Assertions.assertSame;
6   import static org.junit.jupiter.api.Assertions.fail;
7   import static org.junit.runners.MethodSorters.NAME_ASCENDING;
8   
9   import javax.annotation.Resource;
10  import javax.inject.Inject;
11  
12  import org.junit.FixMethodOrder;
13  import org.junit.Test;
14  import org.springframework.beans.factory.annotation.Autowired;
15  import org.springframework.beans.factory.annotation.Value;
16  
17  /**
18   * The Class TestedClassWithAnnotatedDITest.
19   */
20  @FixMethodOrder(NAME_ASCENDING)
21  public final class TestedClassWithAnnotatedDITest {
22  
23      /**
24       * The Class TestedClass1.
25       */
26      static class TestedClass1 {
27  
28          /** The action 2. */
29          @Resource(name = "secondAction")
30          Runnable action2;
31  
32          /** The some value. */
33          @Autowired
34          int someValue;
35  
36          /** The action 1. */
37          @Resource(name = "firstAction")
38          Runnable action1;
39  
40          /** The action 3. */
41          @Resource(name = "thirdAction")
42          Runnable action3;
43  
44          /** The another value. */
45          @Inject
46          int anotherValue;
47  
48          /** The string field with value. */
49          // @Value is supported implicitly (no specific handling for it).
50          @Value("textValue")
51          String stringFieldWithValue;
52  
53          /** The numeric field with value. */
54          @Value("123.45")
55          double numericFieldWithValue;
56  
57          /** The system property. */
58          @Value("#{systemProperties.someProperty}")
59          String systemProperty;
60  
61          /** The an int. */
62          @Value("${anotherSystemProperty}")
63          int anInt;
64  
65          /** The a long value. */
66          @Value("${propertyWithDefault:12345}")
67          long aLongValue; // default value is ignored, left as 0 if no @Injectable or @Tested is found
68      }
69  
70      /**
71       * The Class TestedClass2.
72       */
73      static class TestedClass2 {
74  
75          /** The some value. */
76          final int someValue;
77  
78          /** The action. */
79          final Runnable action;
80  
81          /** The another action. */
82          @Resource
83          Runnable anotherAction;
84  
85          /** The text. */
86          String text;
87  
88          /** The another text. */
89          @Inject
90          String anotherText;
91  
92          /** The optional action. */
93          @Autowired(required = false)
94          Runnable optionalAction;
95  
96          /**
97           * Instantiates a new tested class 2.
98           *
99           * @param someValue
100          *            the some value
101          * @param action
102          *            the action
103          * @param textValue
104          *            the text value
105          */
106         @Autowired
107         TestedClass2(int someValue, Runnable action, String textValue) {
108             this.someValue = someValue;
109             this.action = action;
110             text = textValue;
111         }
112     }
113 
114     /** The tested 1. */
115     @Tested
116     TestedClass1 tested1;
117 
118     /** The tested 2. */
119     @Tested
120     TestedClass2 tested2;
121 
122     /** The first action. */
123     @Injectable
124     Runnable firstAction;
125 
126     /** The some value. */
127     @Injectable
128     final int someValue = 1;
129 
130     /** The action. */
131     @Injectable
132     Runnable action;
133 
134     /** The text value. */
135     @Injectable
136     String textValue = "test";
137 
138     /** The another text. */
139     @Injectable
140     String anotherText = "name2";
141 
142     /** The action 3. */
143     @Injectable
144     Runnable action3; // matches @Resource(name = "thirdAction") by field name, after failing to match on "thirdAction"
145 
146     /**
147      * Inject all annotated injection points.
148      *
149      * @param anotherValue
150      *            the another value
151      * @param secondAction
152      *            the second action
153      * @param anotherAction
154      *            the another action
155      * @param unused
156      *            the unused
157      * @param stringFieldWithValue
158      *            the string field with value
159      * @param numericFieldWithValue
160      *            the numeric field with value
161      * @param systemProperty
162      *            the system property
163      * @param anInt
164      *            the an int
165      * @param aLong
166      *            the a long
167      */
168     @Test
169     public void injectAllAnnotatedInjectionPoints(@Injectable("2") int anotherValue, @Injectable Runnable secondAction,
170             @Injectable Runnable anotherAction, @Injectable("true") boolean unused,
171             @Injectable("test") String stringFieldWithValue, @Injectable("123.45") double numericFieldWithValue,
172             @Injectable("propertyValue") String systemProperty, @Injectable("123") int anInt,
173             @Injectable("987654") long aLong) {
174         assertSame(firstAction, tested1.action1);
175         assertSame(secondAction, tested1.action2);
176         assertSame(action3, tested1.action3);
177         assertEquals(1, tested1.someValue);
178         assertEquals(2, tested1.anotherValue);
179         assertEquals("test", tested1.stringFieldWithValue);
180         assertEquals(123.45, tested1.numericFieldWithValue, 0);
181         assertEquals("propertyValue", tested1.systemProperty);
182         assertEquals(123, tested1.anInt);
183         assertEquals(987654, tested1.aLongValue);
184 
185         assertEquals(1, tested2.someValue);
186         assertSame(action, tested2.action);
187         assertSame(anotherAction, tested2.anotherAction);
188         assertSame(textValue, tested2.text);
189         assertSame(anotherText, tested2.anotherText);
190         assertNull(tested2.optionalAction);
191     }
192 
193     /**
194      * Leave value annotated injection points with default initialization value.
195      *
196      * @param action2
197      *            the action 2
198      * @param anotherAction
199      *            the another action
200      * @param anotherValue
201      *            the another value
202      */
203     @Test
204     public void leaveValueAnnotatedInjectionPointsWithDefaultInitializationValue(@Injectable Runnable action2,
205             @Injectable Runnable anotherAction, @Injectable("2") int anotherValue) {
206         assertNull(tested1.systemProperty);
207         assertEquals(0, tested1.anInt);
208         assertEquals(0, tested1.aLongValue);
209     }
210 
211     /**
212      * Fail for annotated field which lacks an injectable.
213      */
214     @Test(expected = IllegalStateException.class)
215     public void failForAnnotatedFieldWhichLacksAnInjectable() {
216         fail("Must fail before starting");
217     }
218 
219     /**
220      * Fail for annotated field having an injectable of the same type which was already consumed.
221      *
222      * @param secondAction
223      *            the second action
224      */
225     @Test(expected = IllegalStateException.class)
226     public void failForAnnotatedFieldHavingAnInjectableOfTheSameTypeWhichWasAlreadyConsumed(
227             @Injectable Runnable secondAction) {
228         fail("Must fail before starting");
229     }
230 }