4 Replies Latest reply on Aug 25, 2003 8:58 AM by simon555

    test EJB using junit in Eclipse

    simon555 Newbie

      The following is my EJB's source codepackage


      parts.ejb;


      //import javax.ejb.EJBException;
      import javax.ejb.EntityBean;
      import javax.ejb.EntityContext;


      /**
      * @author BEE MICROSYSTEMS
      *
      * @ejb.bean name = "SequenceBean"
      * description = "generation of sequence"
      * display-name = "Sequence Bean"
      * type = "CMP"
      * reentrant = "false"
      * transaction-type = "Container"
      * cmp-version = "2.x"
      * view-type = "local"
      * local-jndi-name = "SequenceBeanHomeLocal"
      *
      * primkey-field = "name"
      *
      *
      *@ejb.env-entry description = "the number of retriev every times"
      * name = "blockSize"
      * type = "java.lang.Integer"
      * value = "10"
      *
      *@ejb.resource-ref res-ref-name = "jdbc/SequencePool"
      * res-type = "javax.sql.DataSource"
      * res-auth = "Container"
      *
      * @jboss.resource-ref res-ref-name = "jdbc/SequencePool"
      *
      * jndi-name = "java:/FirebirdDS"
      *
      *
      *
      *@jboss.persistence create-table = "true"
      * datasource = "java:/FirebirdDS"
      *
      * table-name = "sequence_generate"
      * remove-table = "true"
      *
      *
      *
      *
      *
      *
      *
      *
      *
      *
      *
      *
      */
      public abstract class SequenceBean implements EntityBean {



      private EntityContext ctx;
      // private String name;
      //private int index;
      /**
      *
      */
      public SequenceBean() {
      super();

      }


      public void ejbActivate(){


      }
      /**
      * Deafault create method
      * @return
      * @ejb.create-method
      */
      public String ejbCreate(String name){
      this.setName(name);
      this.setIndex(0);
      return null;


      }
      /**
      *
      * @ejb.persistent-field
      *
      * @jboss.jdbc-type type = "VARCHAR"
      * @jboss.sql-type type = "varchar(32)"
      * @jboss.column-name name = "squ_name"
      *
      *
      *
      *
      * */
      public abstract String getName();
      public abstract void setName(String name);

      /**
      * @ejb.persistent-field
      *
      * @jboss.jdbc-type type = "INTEGER"
      * @jboss.sql-type type = "INTEGER(8)"
      * @jboss.column-name name = "squ_index"
      *
      * */
      public abstract int getIndex();
      public abstract void setIndex(int index);

      /**
      * @param
      * @return
      * @ejb.interface-method view-type = "local"
      * */
      public int getValueAfterIncrementingBy(int blockSize)
      {
      this.setIndex(this.getIndex()+ blockSize);
      return this.getIndex();
      }


      public void ejbLoad(){

      }


      public void ejbPassivate(){

      }


      public void ejbRemove(){

      }


      public void ejbStore(){

      }


      public void setEntityContext(EntityContext context){
      ctx = context;
      }


      public void unsetEntityContext(){


      }

      }


      The following is my testing code
      package parts.tests;

      import javax.naming.InitialContext;
      import javax.naming.NamingException;

      import junit.swingui.TestRunner;
      //import junit.textui.*;
      import junit.framework.TestCase;
      //import junit.framework.*;
      //import java.util.Hashtable;;

      import parts.partsInterface.*;

      /**
      * @author wangjh1
      *
      * To change the template for this generated type comment go to
      * Window>Preferences>Java>Code Generation>Code and Comments
      */
      public class SequenceBeanTest extends TestCase {

      private SequenceBeanLocal sequenceLocal;

      private static SequenceBeanTest ts = new SequenceBeanTest();

      //private HashMap evn = new HashMap();

      public void getConnection(String name)
      {
      getInfo("Running" + this.toString());

      //Hashtable evn = new Hashtable();

      //Adding the initial context to the hashtabel
      try
      {
      getInfo("Create an Initial context");
      InitialContext ic = new InitialContext();
      getInfo("Look for the EJB" + "SequenceBean");
      SequenceBeanLocalHome localHomeInterface = (SequenceBeanLocalHome)ic.lookup("SequenceBeanHomeLocal");
      getInfo("Create a new EJB instance");
      sequenceLocal = localHomeInterface.create(name);

      }
      catch(NamingException e)
      {
      getInfo(e.toString());
      //fail();
      }
      catch(Exception e)
      {
      getInfo("create exception");
      getInfo(e.toString());
      fail();
      }
      }

      public void setUp() throws Exception
      {
      //String name = "orderTest";
      getConnection("orderTest");
      }

      public void testGetValueAfterIncrementingBy()
      {
      try
      {int result = sequenceLocal.getValueAfterIncrementingBy(10);

      getInfo("-----------------------------");
      getInfo("Success without exception");
      assertEquals(10,result);
      }
      catch(Exception e)
      {
      // fail("Fail" + e);
      System.out.println("error" + e);
      }
      }

      public void getInfo(boolean msgObj)
      {
      System.out.println(msgObj);
      }

      public void getInfo(String msg)
      {
      System.out.println(msg);
      }

      public static void main(String args[])
      {
      if(args.length > 0)
      {
      if(args[0].equals("SWING"))
      {
      TestRunner.run(ts.getClass());
      }
      else
      {
      junit.textui.TestRunner.run(ts.getClass());
      }
      }
      else
      {
      //formatting the output
      System.out.println("--------------");
      String className = ts.getClass().getName();
      className = className.substring(className.lastIndexOf(".") + 1);
      System.out.println("Test report of:" + className);
      System.out.println("--------------------------");
      TestRunner.run(ts.getClass());
      }
      }

      }


      My testing approach is:

      1.package the SequenceBean.class into SequenceBean.jar.
      2.put SequenceBean.jar into server\default\lib
      3.start the server
      4.run jUnit with Eclipse

      Result of executed is below:

      RunningtestGetValueAfterIncrementingBy(parts.tests.SequenceBeanTest)
      Create an Initial context
      Look for the EJBSequenceBean
      javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file: java.naming.factory.initial
      errorjava.lang.NullPointerException

      When I stoped the server,rerun the JUnit,result is the seem with above

      My motive is want to test the EJB whether access data from database correct.but it seems like I make a mistake.

      What is the correct approach?

      Thanks.