|
Last update : August 25 2002
Doc for : v1.4
About
What is Cactus ?
News
Changes
Features/Status
Goals
Roadmap/Todo
Contributors
Contributing
Cactus Users
Tested on ...
License
Downloads
Downloads
Documentation
How it works ?
Getting Started
Mock vs Container
Javadocs
FAQ
Howto Guides
Classpath Howto
Config Howto
Migration Howto
TestCase Howto
Jsp Howto
Runner Howto
Security Howto
Ant Howto
HttpUnit Howto
Sample Howto
EJB Howto
IDE Howto
Tomcat Howto
JUnitEE Howto
Support
Bug database
Mailing list
Misc.
Why the name ?
Logo Challenge
Resources
Test Coverage
Stats
Developers
CVS
Coding Conventions
Build results
Release Checklist
|
Forewords |
This tutorial shows how to write test cases for unit testing
EJB methods. The EJB used in this example is a simple Session Bean.
This tutorial assumes the reader has already understood how to use
Cactus to test Servlet. To test an EJB, you need to prepare a war to
contain the cactus setting and test cases, and then prepare a jar of
your ejb. You need to match the JNDI name in the ejb jar and the war.
 |
This is an EJB 1.1 example but the same principles are valid for
EJB 2.0
|
|
EJB Jar |
Step 1 : Prepare the Sample EJB |
The EJB used in this tutorial is a simple Session Bean with a method
for converting Yen to Dollar.
EJB Remote |
package org.apache.cactus.sample.ejb;
import java.rmi.*;
import javax.ejb.*;
public interface Converter extends EJBObject
{
public double convertYenToDollar(double theYenAmount) throws RemoteException;
}
|
|
EJB Home |
package org.apache.cactus.sample.ejb;
import java.rmi.*;
import javax.ejb.*;
public interface ConverterHome extends EJBHome
{
public Converter create() throws RemoteException, CreateException;
}
|
|
EJB Bean |
package org.apache.cactus.sample.ejb;
import javax.ejb.*;
public class ConverterEJB implements SessionBean
{
private SessionContext context;
public double convertYenToDollar(double theYenAmount)
{
return theYenAmount / 100.0;
}
public ConverterEJB()
{
}
public void ejbCreate() throws CreateException
{
}
public void setSessionContext(SessionContext theContext)
{
this.context = theContext;
}
public void ejbActivate()
{
}
public void ejbPassivate()
{
}
public void ejbRemove()
{
}
}
|
|
|
Step 2 : Prepare EJB Deployment Descriptor (ejb-jar.xml) |
Prepare the standard EJB Deployment Descriptor for the Session EJB :
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE ejb-jar PUBLIC
'-//Sun Microsystems, Inc.//DTD Enterprise JavaBeans 1.1//EN'
'http://java.sun.com/j2ee/dtds/ejb-jar_1_1.dtd'>
<ejb-jar>
<display-name>testejb</display-name>
<enterprise-beans>
<session>
<description>Converter Session Bean</description>
<display-name>Converter</display-name>
<ejb-name>Converter</ejb-name>
<home>org.apache.cactus.sample.ejb.ConverterHome</home>
<remote>org.apache.cactus.sample.ejb.Converter</remote>
<ejb-class>org.apache.cactus.sample.ejb.ConverterEJB</ejb-class>
<session-type>Stateless</session-type>
<transaction-type>Container</transaction-type>
</session>
</enterprise-beans>
<assembly-descriptor>
<container-transaction>
<method>
<ejb-name>Converter</ejb-name>
<method-intf>Remote</method-intf>
<method-name>*</method-name>
</method>
<trans-attribute>NotSupported</trans-attribute>
</container-transaction>
</assembly-descriptor>
</ejb-jar>
|
|
Step 3 : Prepare EJB Runtime (only valid for J2EE RI) |
<?xml version="1.0" encoding="ISO-8859-1"?>
<j2ee-ri-specific-information>
<server-name></server-name>
<rolemapping />
<enterprise-beans>
<ejb>
<ejb-name>Converter</ejb-name>
<jndi-name>ejb/Converter</jndi-name>
</ejb>
</enterprise-beans>
</j2ee-ri-specific-information>
|
|
|
Cactus Web Application |
Step 1 : Prepare test code |
package org.apache.cactus.sample.ejb;
import javax.naming.*;
import javax.rmi.*;
import junit.framework.*;
import org.apache.cactus.*;
public class ConverterTest extends ServletTestCase
{
private Converter converter;
public ConverterTest(String name)
{
super(name);
}
public static Test suite()
{
return new TestSuite(ConverterTest.class);
}
public void setUp()
{
Context ctx = new InitialContext();
ConverterHome home = (ConverterHome)
PortableRemoteObject.narrow(ctx.lookup("java:comp/ejb/Converter"),
ConverterHome.class);
this.converter = home.create();
}
public void testConvert() throws Exception
{
double dollar = this.converter.convertYenToDollar(100.0);
assertEquals("dollar", 1.0, dollar, 0.01);
}
}
|
|
Step 2 : Prepare Web Application Deployment Descriptor (web.xml) |
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE web-app
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN"
"http://java.sun.com/j2ee/dtds/web-app_2.2.dtd">
<web-app>
<servlet>
<servlet-name>ServletRedirector</servlet-name>
<servlet-class>org.apache.cactus.server.ServletTestRedirector</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ServletRedirector</servlet-name>
<url-pattern>/ServletRedirector/</url-pattern>
</servlet-mapping>
[...]
<ejb-ref>
<ejb-ref-name>ejb/Converter</ejb-ref-name>
<ejb-ref-type>Session</ejb-ref-type>
<home>org.apache.cactus.sample.ejb.ConverterHome</home>
<remote>org.apache.cactus.sample.ejb.Converter</remote>
</ejb-ref>
</web-app>
|
|
Step 3 : Package Web Application |
Package the web application into a war and then deploy it. Please
note that the ejb-ref-name in the deployment descriptor
need to match with the JNDI name of the test EJB.
|
Step 4 : (Only valid to J2EE RI 1.2.1): War Runtime |
<j2ee-ri-specific-information>
<server-name/>
<rolemapping/>
<web>
<display-name>test</display-name>
<context-root>test</context-root>
<ejb-ref>
<ejb-ref-name>ejb/Converter</ejb-ref-name>
<jndi-name>ejb/Converter</jndi-name>
</ejb-ref>
</web>
</j2ee-ri-specific-information>
|
|
Step 5 : Deploy |
You need to deploy the war and the ejb-jar.xml based on
the deployment procedure of your servlet container and your ejb
container.
|
|
|
|