Chinaunix首页 | 论坛 | 博客
  • 博客访问: 442288
  • 博文数量: 1496
  • 博客积分: 79800
  • 博客等级: 大将
  • 技术积分: 9940
  • 用 户 组: 普通用户
  • 注册时间: 2008-09-09 13:22
文章分类

全部博文(1496)

文章存档

2011年(1)

2008年(1495)

我的朋友

分类:

2008-09-09 17:21:48

    本文面向的是那些想要对他们的 EJB 进行单元测试以及为这些 EJB 开发测试案例的 VisualAge? for ? 用户。本文基于 VisualAge for 3.5.3 和 JUnit 3.7。文章描述了 JUnit、对 EJB 进行单元测试的难点以及开发测试案例时涉及到的相关步骤。

  引言

  本文面向的是那些想要对他们的 EJB 进行单元测试以及为这些 EJB 开发测试案例的 VisualAge? for Java? 用户。本文基于 VisualAge for Java 3.5.3 和 JUnit 3.7。文章描述了 JUnit、对 EJB 进行单元测试的难点以及开发测试案例时涉及到的相关步骤。

  单元测试是以程序员的视角来编写的。单元测试确保一个类的某个特定的方法能成功地执行一组特定的任务。每个测试确定一个方法在给定已知的输入时能产生预期的输出。有效测试是有效编程的一个基本的组成部分。通过使用 JUnit 测试框架,您能容易地并且逐步地构建一个测试套件,这个测试套件能帮助您调节工作进度、发现不希望出现的副作用并把精力集中在开发工作上。

  编写 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 {
  .....
  }
  }
    
    使用下列步骤创建一个 EJB 测试案例。

  通过继承 JUnit.framework.TestCase 类创建一个测试类。命名约定:如果 bean 的名称是 SampleEjbBean ,则将测试类命名为 SampleEjbBeanTest 。例如:

  public class SampleEjbBeanTest extends JUnit.framework.TestCase{ 。

  创建 Bean 的一个 remoteInterface 类型的类变量。例如:

  SampleEjb remoteInterface

  创建测试类的一个静态实例:

    

     static {

  instance = new SampleEjbBeanTest("");

  }


    
    因为该实例被用来作为 TestRunner 的 run 方法的一个参数以执行 TestClass.main 方法和测试案例,所以您可以在 SwingUI 或者 TextUI 中执行测试案例:

    

     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 中。例如:

  env.put(Context.INITIAL_CONTEXT_FACTORY,"com.ibm.ejs.ns.jndi.CNInitialContextFactory

  将 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();
  }
  }

    
   

 

【责编:Zenghui】

--------------------next---------------------
     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());
  }
  }
--------------------next---------------------
     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();
  }
  }
--------------------next---------------------

阅读(131) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~