1919import static org .junit .Assert .assertEquals ;
2020import static org .junit .Assert .assertNotNull ;
2121import static org .junit .Assert .assertNull ;
22+ import static org .junit .Assert .assertSame ;
2223import static org .junit .Assert .assertTrue ;
2324
2425import javax .annotation .Resource ;
26+ import javax .inject .Inject ;
2527
2628import org .junit .Test ;
2729import org .junit .runner .RunWith ;
4850 * <ul>
4951 * <li>{@link ContextConfiguration @ContextConfiguration}</li>
5052 * <li>{@link Autowired @Autowired}</li>
53+ * <li>{@link Inject @Inject}</li>
5154 * <li>{@link Qualifier @Qualifier}</li>
5255 * <li>{@link Resource @Resource}</li>
5356 * <li>{@link ApplicationContextAware}</li>
5962 * {@link ContextConfiguration#locations() locations} are explicitly declared
6063 * and since the {@link ContextConfiguration#loader() ContextLoader} is left set
6164 * to the default value of {@link GenericXmlContextLoader}, this test class's
62- * dependencies will be injected via {@link Autowired @Autowired} and
63- * {@link Resource @Resource} from beans defined in the
64- * {@link ApplicationContext} loaded from the default classpath resource:
65- * <code>"/org/springframework/test/context/junit/SpringJUnit4ClassRunnerAppCtxTests-context.xml"</code>.
65+ * dependencies will be injected via {@link Autowired @Autowired},
66+ * {@link Inject @Inject}, and {@link Resource @Resource} from beans defined in
67+ * the {@link ApplicationContext} loaded from the default classpath resource:
68+ *
69+ * <code>"/org/springframework/test/context/junit/SpringJUnit4ClassRunnerAppCtxTests-context.xml"</code>
70+ * .
6671 * </p>
6772 *
6873 * @author Sam Brannen
@@ -93,12 +98,15 @@ public class SpringJUnit4ClassRunnerAppCtxTests implements ApplicationContextAwa
9398 private Employee employee ;
9499
95100 @ Autowired
96- private Pet pet ;
101+ private Pet autowiredPet ;
102+
103+ @ Inject
104+ private Pet injectedPet ;
97105
98106 @ Autowired (required = false )
99107 protected Long nonrequiredLong ;
100108
101- @ Resource ()
109+ @ Resource
102110 protected String foo ;
103111
104112 protected String bar ;
@@ -153,11 +161,14 @@ public final void verifyBeanNameSet() {
153161 }
154162
155163 @ Test
156- public final void verifyAnnotationAutowiredFields () {
164+ public final void verifyAnnotationAutowiredAndInjectedFields () {
157165 assertNull ("The nonrequiredLong field should NOT have been autowired." , this .nonrequiredLong );
158166 assertEquals ("The quux field should have been autowired via @Autowired and @Qualifier." , "Quux" , this .quux );
159- assertNotNull ("The pet field should have been autowired." , this .pet );
160- assertEquals ("Fido" , this .pet .getName ());
167+ assertNotNull ("The pet field should have been autowired." , this .autowiredPet );
168+ assertNotNull ("The pet field should have been injected." , this .injectedPet );
169+ assertEquals ("Fido" , this .autowiredPet .getName ());
170+ assertEquals ("Fido" , this .injectedPet .getName ());
171+ assertSame ("@Autowired and @Inject pet should be the same object." , this .autowiredPet , this .injectedPet );
161172 }
162173
163174 @ Test
@@ -176,4 +187,4 @@ public final void verifyResourceAnnotationWiredMethods() {
176187 assertEquals ("The bar method should have been wired via @Resource." , "Bar" , this .bar );
177188 }
178189
179- }
190+ }
0 commit comments