Solved

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

Posted on 2006-11-21
11
393 Views
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.

Thanks
0
Comment
Question by:LeanMoreTryMore
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 5
11 Comments
 
LVL 16

Expert Comment

by:suprapto45
ID: 17992705
For me,

Last time, when I was using EJB, I used http://www.junitee.org/ and it works great.

http://www.junitee.org/tutorial.html

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

David
0
 

Author Comment

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

please help
0
 

Author Comment

by:LeanMoreTryMore
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

CustomerAccount.java
CustomerAccountBean.java
CustomerAccountHome.java
CustomerAccountLocal.java
CustomerAccountLocalHome

==
The IDE create the Junit template like below:

package com.fujitsu.au;

import junit.framework.TestCase;

public class TestInsert extends TestCase {

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

}

===
Any idea what else i need to add to the above TestInsert
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 16

Expert Comment

by:suprapto45
ID: 17993063
Hi,

The URL that I gave you is the sample to test on EJB.
http://www.junitee.org/tutorial.html

Basically, you need to create one more web application to test the EJB. If you need more detailed of the steps, look at
http://www.junitee.org/usersguide.html

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

David
0
 
LVL 16

Expert Comment

by:suprapto45
ID: 17993086
What IDE are you using?
0
 

Author Comment

by:LeanMoreTryMore
ID: 17993091
SAP NetWeaver (shit product)
0
 

Author Comment

by:LeanMoreTryMore
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",
                 "com.sun.jndi.cosnaming.CNCtxFactory");
      props.put ("java.naming.provider.url",
                 "iiop://localhost:3700");
      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());
      cust.setBalance(99.00);
      System.out.println("new balance: " + cust.getBalance());

      Collection customers = customerHome.findByBalance(50);
      Iterator it = customers.iterator();
      while(it.hasNext()) {
        cust = (Customer) it.next();
        System.out.println(cust.getFirstname());
      }
    } catch (Exception e) {
      e.printStackTrace();
    }

  }

}
0
 
LVL 16

Expert Comment

by:suprapto45
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....
0
 
LVL 16

Accepted Solution

by:
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
http://www.laliluna.de/eclipse-junit-testing-tutorial.html

But that is for Eclipse :(
0
 

Author Comment

by:LeanMoreTryMore
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 com.fujitsu.au.servicerequest.util.ServiceRequestModel;
import com.fujitsu.au.servicerequest.util.ServiceRequestException;
import com.fujitsu.au.servicerequest.ServiceRequestProcessorHome;
import com.fujitsu.au.servicerequest.ServiceRequestProcessor;

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 =
                         (ServiceRequestProcessorHome)PortableRemoteObject.narrow(einRef,ServiceRequestProcessorHome.class);
                  this.sr = home.create();
            }

            protected void tearDown() throws Exception      {
                  this.sr = null;
            }

        public void testInsertRec() throws RemoteException, ServiceRequestException {            
              Date todayDate = new Date();
                  ServiceRequestModel srRec = this.sr.saveServiceRequest("DOR-20061123-01",
                                                                         "David Smith",
                                                                         "Address Line 1",
                                                                         "Address Line 2",
                                                                         "Suburb",
                                                                         "Sydney",
                                                                         "2000",
                                                                         "Australia",
                                                                         "David.Smith@yahoo.com",
                                                                         "My test Message",
                                                                         "Submit",
                                                                         "HELLO",
                                                                         todayDate,
                                                                         "David");
        }
}
0
 
LVL 16

Expert Comment

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

David
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Maven Project: Hibernate Dependencies Conflict 10 50
servlet example 11 59
running on tomcat not jboss eap 7.0 3 31
Where to place postgres JDBC driver jar on tomcat 8 30
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

733 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