SpringJUnit4ClassRunnerAppCtxTests now verifies seamless support for using @Inject in addition to @Autowired, etc.

This commit is contained in:
Sam Brannen 2009-10-05 22:08:01 +00:00
parent 0a843e60a6
commit 710ae3a9d2
5 changed files with 30 additions and 10 deletions

View File

@ -15,6 +15,7 @@
<classpathentry combineaccessrules="false" kind="src" path="/org.springframework.web.portlet"/> <classpathentry combineaccessrules="false" kind="src" path="/org.springframework.web.portlet"/>
<classpathentry combineaccessrules="false" kind="src" path="/org.springframework.web.servlet"/> <classpathentry combineaccessrules="false" kind="src" path="/org.springframework.web.servlet"/>
<classpathentry kind="var" path="IVY_CACHE/javax.activation/com.springsource.javax.activation/1.1.0/com.springsource.javax.activation-1.1.0.jar" sourcepath="/IVY_CACHE/javax.activation/com.springsource.javax.activation/1.1.0/com.springsource.javax.activation-sources-1.1.0.jar"/> <classpathentry kind="var" path="IVY_CACHE/javax.activation/com.springsource.javax.activation/1.1.0/com.springsource.javax.activation-1.1.0.jar" sourcepath="/IVY_CACHE/javax.activation/com.springsource.javax.activation/1.1.0/com.springsource.javax.activation-sources-1.1.0.jar"/>
<classpathentry kind="var" path="IVY_CACHE/javax.inject/com.springsource.javax.inject/0.9.0.PFD/com.springsource.javax.inject-0.9.0.PFD.jar" sourcepath="/IVY_CACHE/javax.inject/com.springsource.javax.inject/0.9.0.PFD/com.springsource.javax.inject-sources-0.9.0.PFD.jar"/>
<classpathentry kind="var" path="IVY_CACHE/javax.persistence/com.springsource.javax.persistence/1.0.0/com.springsource.javax.persistence-1.0.0.jar" sourcepath="/IVY_CACHE/javax.persistence/com.springsource.javax.persistence/1.0.0/com.springsource.javax.persistence-sources-1.0.0.jar"/> <classpathentry kind="var" path="IVY_CACHE/javax.persistence/com.springsource.javax.persistence/1.0.0/com.springsource.javax.persistence-1.0.0.jar" sourcepath="/IVY_CACHE/javax.persistence/com.springsource.javax.persistence/1.0.0/com.springsource.javax.persistence-sources-1.0.0.jar"/>
<classpathentry kind="var" path="IVY_CACHE/javax.portlet/com.springsource.javax.portlet/2.0.0/com.springsource.javax.portlet-2.0.0.jar"/> <classpathentry kind="var" path="IVY_CACHE/javax.portlet/com.springsource.javax.portlet/2.0.0/com.springsource.javax.portlet-2.0.0.jar"/>
<classpathentry kind="var" path="IVY_CACHE/javax.servlet/com.springsource.javax.servlet/2.5.0/com.springsource.javax.servlet-2.5.0.jar" sourcepath="/IVY_CACHE/javax.servlet/com.springsource.javax.servlet/2.5.0/com.springsource.javax.servlet-sources-2.5.0.jar"/> <classpathentry kind="var" path="IVY_CACHE/javax.servlet/com.springsource.javax.servlet/2.5.0/com.springsource.javax.servlet-2.5.0.jar" sourcepath="/IVY_CACHE/javax.servlet/com.springsource.javax.servlet/2.5.0/com.springsource.javax.servlet-sources-2.5.0.jar"/>

View File

@ -21,6 +21,7 @@
<dependencies> <dependencies>
<dependency org="javax.activation" name="com.springsource.javax.activation" rev="1.1.0" conf="provided->compile"/> <dependency org="javax.activation" name="com.springsource.javax.activation" rev="1.1.0" conf="provided->compile"/>
<dependency org="javax.el" name="com.springsource.javax.el" rev="1.0.0" conf="provided->compile"/> <dependency org="javax.el" name="com.springsource.javax.el" rev="1.0.0" conf="provided->compile"/>
<dependency org="javax.inject" name="com.springsource.javax.inject" rev="0.9.0.PFD" conf="test->compile"/>
<dependency org="javax.persistence" name="com.springsource.javax.persistence" rev="1.0.0" conf="provided->compile"/> <dependency org="javax.persistence" name="com.springsource.javax.persistence" rev="1.0.0" conf="provided->compile"/>
<dependency org="javax.portlet" name="com.springsource.javax.portlet" rev="2.0.0" conf="provided->compile"/> <dependency org="javax.portlet" name="com.springsource.javax.portlet" rev="2.0.0" conf="provided->compile"/>
<dependency org="javax.servlet" name="com.springsource.javax.servlet" rev="2.5.0" conf="provided->compile"/> <dependency org="javax.servlet" name="com.springsource.javax.servlet" rev="2.5.0" conf="provided->compile"/>

View File

@ -25,6 +25,12 @@
<version>1.0</version> <version>1.0</version>
<scope>provided</scope> <scope>provided</scope>
</dependency> </dependency>
<dependency>
<groupId>javax.inject</groupId>
<artifactId>com.springsource.javax.inject</artifactId>
<version>0.9.0.PFD</version>
<scope>test</scope>
</dependency>
<dependency> <dependency>
<groupId>javax.persistence</groupId> <groupId>javax.persistence</groupId>
<artifactId>persistence-api</artifactId> <artifactId>persistence-api</artifactId>

View File

@ -19,9 +19,11 @@ package org.springframework.test.context.junit4;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull; import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
import javax.annotation.Resource; import javax.annotation.Resource;
import javax.inject.Inject;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
@ -48,6 +50,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 Inject @Inject}</li>
* <li>{@link Qualifier @Qualifier}</li> * <li>{@link Qualifier @Qualifier}</li>
* <li>{@link Resource @Resource}</li> * <li>{@link Resource @Resource}</li>
* <li>{@link ApplicationContextAware}</li> * <li>{@link ApplicationContextAware}</li>
@ -59,10 +62,12 @@ import org.springframework.test.context.support.GenericXmlContextLoader;
* {@link ContextConfiguration#locations() locations} are explicitly declared * {@link ContextConfiguration#locations() locations} are explicitly declared
* and since the {@link ContextConfiguration#loader() ContextLoader} is left set * and since the {@link ContextConfiguration#loader() ContextLoader} is left set
* 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},
* {@link Resource @Resource} from beans defined in the * {@link Inject @Inject}, and {@link Resource @Resource} from beans defined in
* {@link ApplicationContext} loaded from the default classpath resource: * the {@link ApplicationContext} loaded from the default classpath resource:
* <code>&quot;/org/springframework/test/context/junit/SpringJUnit4ClassRunnerAppCtxTests-context.xml&quot;</code>. *
* <code>&quot;/org/springframework/test/context/junit/SpringJUnit4ClassRunnerAppCtxTests-context.xml&quot;</code>
* .
* </p> * </p>
* *
* @author Sam Brannen * @author Sam Brannen
@ -93,12 +98,15 @@ public class SpringJUnit4ClassRunnerAppCtxTests implements ApplicationContextAwa
private Employee employee; private Employee employee;
@Autowired @Autowired
private Pet pet; private Pet autowiredPet;
@Inject
private Pet injectedPet;
@Autowired(required = false) @Autowired(required = false)
protected Long nonrequiredLong; protected Long nonrequiredLong;
@Resource() @Resource
protected String foo; protected String foo;
protected String bar; protected String bar;
@ -153,11 +161,14 @@ public class SpringJUnit4ClassRunnerAppCtxTests implements ApplicationContextAwa
} }
@Test @Test
public final void verifyAnnotationAutowiredFields() { public final void verifyAnnotationAutowiredAndInjectedFields() {
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); 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.autowiredPet);
assertEquals("Fido", this.pet.getName()); assertNotNull("The pet field should have been injected.", this.injectedPet);
assertEquals("Fido", this.autowiredPet.getName());
assertEquals("Fido", this.injectedPet.getName());
assertSame("@Autowired and @Inject pet should be the same object.", this.autowiredPet, this.injectedPet);
} }
@Test @Test

View File

@ -24,6 +24,7 @@
<orderEntry type="library" name="Commons Logging" level="project" /> <orderEntry type="library" name="Commons Logging" level="project" />
<orderEntry type="library" name="EasyMock" level="project" /> <orderEntry type="library" name="EasyMock" level="project" />
<orderEntry type="library" name="javax.el" level="project" /> <orderEntry type="library" name="javax.el" level="project" />
<orderEntry type="library" name="javax.inject" level="project" />
<orderEntry type="library" name="JUnit" level="project" /> <orderEntry type="library" name="JUnit" level="project" />
<orderEntry type="module-library"> <orderEntry type="module-library">
<library> <library>