I am trying to test an EJB3 using annotation. The problem is the
injected value is always null. In my junit test, I setup the OpenEJB
environment, and then get the EJB via a JNDI lookup and then via an
annotated test class. The JNDI lookup always works.
I suspect this is a simple configuration error, but I am not sure where
the error is.
Below are copies of the tests, maven pom, and annotated test class.
Suggestions?
Paul Spencer
***
* pom.xml
***
<project>
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.foo.helloworld</groupId>
<artifactId>helloworld-master-pom</artifactId>
<version>1.0-SNAPSHOT</version>
<relativePath>../helloworld/pom.xml</relativePath>
</parent>
<artifactId>helloworld-ejb</artifactId>
<name>Hello World EJB</name>
<packaging>ejb</packaging>
<build>
<plugins>
<plugin>
<artifactId>maven-ejb-plugin</artifactId>
<configuration>
<ejbVersion>3.0</ejbVersion>
<generateClient>true</generateClient>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.apache.openejb</groupId>
<artifactId>openejb-ejbd</artifactId>
<version>3.0.0-SNAPSHOT</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.openejb</groupId>
<artifactId>openejb-core</artifactId>
<version>3.0.0-SNAPSHOT</version>
<scope>test</scope>
</dependency>
</dependencies>
<repositories>
<repository>
<id>openejb-3rdparty-builds</id>
<name>3rd Party Build Repository</name>
<url>http://svn.apache.org/repos/asf/openejb/repo/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
<repository>
<id>Apache Snapshot Repo</id>
<name>Apache Snapshot Repo</name>
<url>http://people.apache.org/repo/m2-snapshot-repository/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
</project>
***
* Annotated test class
***
class MyTestClass {
@EJB
CompanyAdminLocal localBean;
@EJB(name="CompanyAdminImplRemote")
CompanyAdminRemote remoteBean;
@EJB
CompanyAdminImpl defaultBean;
public CompanyAdminLocal getLocalBean() {
return localBean;
}
public CompanyAdminRemote getRemoteBean() {
return remoteBean;
}
public CompanyAdmin getDefaultBean() {
return defaultBean;
}
}
***
* Unit Test
***
public class CompanyAdminBeanEjbTest extends TestCase {
InitialContext localContext;
InitialContext remoteContext;
protected void setUp() throws Exception {
Properties properties = new Properties();
properties.setProperty(Context.INITIAL_CONTEXT_FACTORY,
"org.apache.openejb.client.LocalInitialContextFactory");
properties.setProperty("openejb.deployments.classpath.include",
".*beans.*");
properties.setProperty("openejb.deployments.classpath.exclude",
".*telephone.*");
properties.setProperty("openejb.embedded.remotable", "true");
new InitialContext(properties);
Properties localProperties = new Properties();
localProperties.setProperty(Context.INITIAL_CONTEXT_FACTORY,
"org.apache.openejb.client.LocalInitialContextFactory");
localContext = new InitialContext(localProperties);
Properties remoteProperties = new Properties();
remoteProperties.setProperty(Context.INITIAL_CONTEXT_FACTORY,
"org.apache.openejb.client.LocalInitialContextFactory");
remoteContext = new InitialContext(remoteProperties);
}
public void testLocalCompanyAdminBean() throws Exception {
CompanyAdmin companyAdmin = (CompanyAdmin) localContext
.lookup("CompanyAdminImplLocal");
assertNotNull("Has CompanyAdminBean", companyAdmin);
}
public void testRemoteCompanyAdminBean() throws Exception {
CompanyAdmin companyAdmin = (CompanyAdmin) remoteContext
.lookup("CompanyAdminImplRemote");
assertNotNull("Has CompanyAdminBean", companyAdmin);
}
public void testLocalMyTestClass() throws Exception {
MyTestClass testClass = new MyTestClass();
assertNotNull("Has local bean", testClass.getLocalBean());
}
public void testRemoteMyTestClass() throws Exception {
MyTestClass testClass = new MyTestClass();
assertNotNull("Has Remote bean", testClass.getRemoteBean());
}
public void testDefaultMyTestClass() throws Exception {
MyTestClass testClass = new MyTestClass();
assertNotNull("Has Remote bean", testClass.getDefaultBean());
}
public void testCompanyName() throws Exception {
CompanyAdminLocal companyAdmin = (CompanyAdminLocal)
localContext
.lookup("CompanyAdminBean");
String newName = "New Company Name";
companyAdmin.setCompanyName(newName);
assertEquals("Company Name", newName,
companyAdmin.getCompanyName());
}
}