Spring TestContext Framework autowiring tests now include an explicit test for the @Autowired-@Qualifier combination.

This commit is contained in:
Sam Brannen 2009-05-17 10:22:03 +00:00
parent d13567bc8f
commit 76aa8b2119
2 changed files with 18 additions and 6 deletions

View File

@ -20,4 +20,8 @@
<constructor-arg value="Bar" /> <constructor-arg value="Bar" />
</bean> </bean>
<bean id="quux" class="java.lang.String">
<constructor-arg value="Quux" />
</bean>
</beans> </beans>

View File

@ -25,13 +25,13 @@ import javax.annotation.Resource;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.springframework.beans.BeansException; import org.springframework.beans.BeansException;
import org.springframework.beans.Employee; import org.springframework.beans.Employee;
import org.springframework.beans.Pet; import org.springframework.beans.Pet;
import org.springframework.beans.factory.BeanNameAware; import org.springframework.beans.factory.BeanNameAware;
import org.springframework.beans.factory.InitializingBean; import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware; import org.springframework.context.ApplicationContextAware;
import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.ContextConfiguration;
@ -48,6 +48,7 @@ import org.springframework.test.context.support.GenericXmlContextLoader;
* <ul> * <ul>
* <li>{@link ContextConfiguration @ContextConfiguration}</li> * <li>{@link ContextConfiguration @ContextConfiguration}</li>
* <li>{@link Autowired @Autowired}</li> * <li>{@link Autowired @Autowired}</li>
* <li>{@link Qualifier @Qualifier}</li>
* <li>{@link Resource @Resource}</li> * <li>{@link Resource @Resource}</li>
* <li>{@link ApplicationContextAware}</li> * <li>{@link ApplicationContextAware}</li>
* <li>{@link BeanNameAware}</li> * <li>{@link BeanNameAware}</li>
@ -60,7 +61,8 @@ import org.springframework.test.context.support.GenericXmlContextLoader;
* to the default value of {@link GenericXmlContextLoader}, this test class's * to the default value of {@link GenericXmlContextLoader}, this test class's
* dependencies will be injected via {@link Autowired @Autowired} and * dependencies will be injected via {@link Autowired @Autowired} and
* {@link Resource @Resource} from beans defined in the * {@link Resource @Resource} from beans defined in the
* {@link ApplicationContext} loaded from the default classpath resource: &quot;<code>/org/springframework/test/context/junit/SpringJUnit4ClassRunnerAppCtxTests-context.xml</code>&quot;. * {@link ApplicationContext} loaded from the default classpath resource:
* <code>&quot;/org/springframework/test/context/junit/SpringJUnit4ClassRunnerAppCtxTests-context.xml&quot;</code>.
* </p> * </p>
* *
* @author Sam Brannen * @author Sam Brannen
@ -77,6 +79,7 @@ public class SpringJUnit4ClassRunnerAppCtxTests implements ApplicationContextAwa
/** /**
* Default resource path for the application context configuration for * Default resource path for the application context configuration for
* {@link SpringJUnit4ClassRunnerAppCtxTests}: * {@link SpringJUnit4ClassRunnerAppCtxTests}:
*
* <code>&quot;/org/springframework/test/context/junit4/SpringJUnit4ClassRunnerAppCtxTests-context.xml&quot;</code> * <code>&quot;/org/springframework/test/context/junit4/SpringJUnit4ClassRunnerAppCtxTests-context.xml&quot;</code>
*/ */
public static final String DEFAULT_CONTEXT_RESOURCE_PATH = "/org/springframework/test/context/junit4/SpringJUnit4ClassRunnerAppCtxTests-context.xml"; public static final String DEFAULT_CONTEXT_RESOURCE_PATH = "/org/springframework/test/context/junit4/SpringJUnit4ClassRunnerAppCtxTests-context.xml";
@ -100,6 +103,10 @@ public class SpringJUnit4ClassRunnerAppCtxTests implements ApplicationContextAwa
protected String bar; protected String bar;
@Autowired
@Qualifier("quux")
protected String quux;
// ------------------------------------------------------------------------| // ------------------------------------------------------------------------|
@ -148,6 +155,7 @@ public class SpringJUnit4ClassRunnerAppCtxTests implements ApplicationContextAwa
@Test @Test
public final void verifyAnnotationAutowiredFields() { public final void verifyAnnotationAutowiredFields() {
assertNull("The nonrequiredLong field should NOT have been autowired.", this.nonrequiredLong); assertNull("The nonrequiredLong field should NOT have been autowired.", this.nonrequiredLong);
assertEquals("The quux field should have been autowired via @Autowired and @Qualifier.", "Quux", this.quux);
assertNotNull("The pet field should have been autowired.", this.pet); assertNotNull("The pet field should have been autowired.", this.pet);
assertEquals("Fido", this.pet.getName()); assertEquals("Fido", this.pet.getName());
} }