Junit - how do i use the Junit to test for ejb function

Posted on 2006-11-21
Last Modified: 2013-11-24
I have a ejb applicaiton used to insert the record into the database. It completed.

Now i want to use junit to test for inserting a record.

For my i dont have any idea how to do it.

Please advise.

Question by:LeanMoreTryMore
  • 6
  • 5
LVL 16

Expert Comment

ID: 17992705
For me,

Last time, when I was using EJB, I used and it works great.

However, other may have other suggestions so you may be waiting for alternative solution


Author Comment

ID: 17993021
Thanks. but there is no sample for testing ejb.

please help

Author Comment

ID: 17993061
I need to test the CMP entity bean for inserting a record into the database

The CMP entity bean contains the following classes

The IDE create the Junit template like below:


import junit.framework.TestCase;

public class TestInsert extends TestCase {

      public TestInsert(String arg0) {


Any idea what else i need to add to the above TestInsert
LVL 16

Expert Comment

ID: 17993063

The URL that I gave you is the sample to test on EJB.

Basically, you need to create one more web application to test the EJB. If you need more detailed of the steps, look at

It may be a bit complicated to set up but should be doable.

LVL 16

Expert Comment

ID: 17993086
What IDE are you using?
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.


Author Comment

ID: 17993091
SAP NetWeaver (shit product)

Author Comment

ID: 17993109
Can I test without creating the web page?

I remember when I was at Sun's J2EE course, they can test the EJB function before creating web page.

like below: ( From sun's j2ee course I attented). But when i do the same thing, it does not work from within the IDE

Test for Searching
package bank;

import javax.naming.*;
import javax.rmi.*;
import java.util.*;

public class CustomerTest {

  public static void main(String [] args) {
    try {
      Hashtable props = new Hashtable();
      props.put ("java.naming.factory.initial",
      props.put ("java.naming.provider.url",
      Context c = new InitialContext(props);

      Object obj = c.lookup("ejb/Customer");
      CustomerHome customerHome =
        (CustomerHome) PortableRemoteObject.narrow(obj, CustomerHome.class);

      Customer cust = customerHome.findByPrimaryKey(new Integer(1));
      System.out.println("firstname: " + cust.getFirstname());
      System.out.println("lastname: " + cust.getLastname());
      System.out.println("address: " + cust.getAddress());
      System.out.println("phone: " + cust.getPhone());
      System.out.println("balance: " + cust.getBalance());
      System.out.println("new balance: " + cust.getBalance());

      Collection customers = customerHome.findByBalance(50);
      Iterator it = customers.iterator();
      while(it.hasNext()) {
        cust = (Customer);
    } catch (Exception e) {


LVL 16

Expert Comment

ID: 17993139
>>"SAP NetWeaver (shit product)"
Grr...never used that...can't really help on this.

>>"like below: ( From sun's j2ee course I attented). But when i do the same thing, it does not work from within the IDE"
It is not JUnit but it is a simple java class that called EJB and do the necessary invokation.

Give me a sec....
LVL 16

Accepted Solution

suprapto45 earned 400 total points
ID: 17993207
I can't really help you much on this as I have limited knowledge of your IDE.

>>"I remember when I was at Sun's J2EE course, they can test the EJB function before creating web page."
This could be a good starting point to you albeit it is not JUnit. Try to run it as a simple Java application. The error may come out that you cannot find the EJB properly.

The good sample of JUnit is

But that is for Eclipse :(

Author Comment

ID: 17999400
When i compile the below, it says "junit.framework never use" WHY.

import junit.framework.TestCase;
import java.rmi.RemoteException;
import java.util.Date;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.rmi.PortableRemoteObject;


import junit.framework.TestCase;

public class TestInsert extends TestCase {

        protected ServiceRequestProcessor sr;

          public TestInsert(String name) { super(name); }

            protected void setUp() throws Exception {
                  Context jndiContext = new InitialContext();
                  Object einRef = jndiContext.lookup("java:comp/env/ejb/ServiceRequestProcessorBean");
                  ServiceRequestProcessorHome home =
         = home.create();

            protected void tearDown() throws Exception      {
         = null;

        public void testInsertRec() throws RemoteException, ServiceRequestException {            
              Date todayDate = new Date();
                  ServiceRequestModel srRec ="DOR-20061123-01",
                                                                         "David Smith",
                                                                         "Address Line 1",
                                                                         "Address Line 2",
                                                                         "My test Message",
LVL 16

Expert Comment

ID: 18000378
>>"When i compile the below, it says "junit.framework never use" WHY."
Can you post the full stack trace of the exception?


Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
groupSum6 challenge 6 77
JAVA part two 5 58
VB Script to add site to Java Exception List 4 53
hibernate jars 4 9
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now