使用JUnit对EJB进行单元测试(希赛
于 08-03-26 22:41 通过网页 帖子号: 14366
本文面向的是那些想要对他们的 EJB 进行单元测试以及为这些 EJB
© Copyright International Business Machines Corporation 2002. All rights reserved.
引言
本文面向的是那些想要对他们的 EJB 进行单元测试以及为这些 EJB 开发测试案例的 VisualAge® for Java™ 用户。本文基于 VisualAge for Java 3.5.3 和 JUnit 3.7。文章描述了 JUnit、对 EJB 进行单元测试的难点以及开发测试案例时涉及到的相关步骤。
单元测试是以程序员
编写 EJBs 测试案例
这里是一个关于 EJB 的示例,该 EJB 带有一个名为 addition 的业务方法,该方法以两个整型变量作为输入,将它们相加后返回结果:
/**
* This is a Session Bean Class
*/
public class SampleEjbBean implements SessionBean {
private javax.ejb.SessionContext mySessionCtx = null;
final static long serialVersionUID = 3206093459760846163L;
/**
* Insert the method's description here.
* Creation date: (8/10/02 1:16:33 PM)
* @return int
* @param a int
*/
//The Business method
public int addition(int a,int b) {
return a+b;
}
public void ejbActivate() throws java.rmi.RemoteException {...}
public void ejbCreate() throws javax.ejb.CreateException, java.rmi.RemoteException {...}public void ejbPassivate() throws java.rmi.RemoteException {...}
public void ejbRemove() throws java.rmi.RemoteException {...}
public javax.ejb.SessionContext getSessionContext() {
.....
}
public void setSessionContext(javax.ejb.SessionContext ctx) throws java.rmi.RemoteException {
.....
}
}
通过继承 JUnit.framework.TestCase 类创建一个测试类。命名约定:如果 bean 的名称是 SampleEjbBean ,则将测试类命名为 SampleEjbBeanTest 。例如:
public class SampleEjbBeanTest extends JUnit.framework.TestCase{ 。
创建 Bean 的一个 remoteInterface 类型的类变量。例如:
SampleEjb remoteInterface
创建测试类的一个静态实例: static {
instance = new SampleEjbBeanTest("");
}
public static void main(String args[])
{
if (args.length > 0){
if (args[0].equals("SWING")) {
JUnit .swingui.TestRunner.run(instance.getClass());
}
else {
JUnit .textui.TestRunner.run(instance.getClass());
}
}
else {
//formatting the Output
System.out.println("************************************");
String className = instance.getClass().getName();
className = className.substring(className.lastIndexOf(".")+1);
System.out.println("Test Report of:"+className);
System.out.println("************************************");
JUnit.textui.TestRunner.run(instance.getClass());
}
}
接着,创建一个用于连接运行在服务器上的 EJB bean 的方法并为远程接口创建句柄:
将初始上下文添加到 HashMap 中。例如:
将 URL 添加到 HashMap 中。例如:
env.put(Context.PROVIDER_URL, "iiop://localhost:900");
创建 InitialContext 对象。例如:
javax.naming.InitialContext ic =new javax.naming.InitialContext(env);
通过在命名服务器中查找 EJB Alias 名称来构造 Bean 的一个 homeInterface 例如:
SampleEjbHome homeInterface = (SampleEjbHome) ic.lookup("SampleEjb");
通过调用 homeInterface 的 create 方法创建一个 remoteInterface 。 例如:
remoteInterface = homeInterface.create(); Public void getConnection()
{
getinfo("Running " + this.toString());
java.util.Hashtable env = new Hashtable();
//Adding the Initial Context to the HashMap.
env.put(Context.INITIAL_CONTEXT_FACTORY,"com.ibm.ejs.ns.jndi.CNInitialContextFactory
");
env.put(Context.PROVIDER_URL, "iiop://localhost:900");
try
{
getinfo("Creating an initial context");
javax.naming.InitialContext ic =new javax.naming.InitialContext(env);
getinfo("Looking for the EJB " + "SampleEjb");
SampleEjbHome homeInterface =
(SampleEjbHome) ic.lookup("SampleEjb");
getinfo("Creating a new EJB instance");
remoteInterface = homeInterface.create();
}
catch (NamingException e)
{
getinfo(e.toString());
fail();
}
catch (Exception e)
{
getinfo("Create Exception");
getinfo(e.toString());
fail();
}
}
楼主