Newsletter sign-up
View all newsletters

Enterprise Java Newsletter
Stay up to date on the latest tutorials and Java community news posted on JavaWorld

Sponsored Links

Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs

Build, deploy, and test EJB components in just a few seconds

OpenEJB simplifies Enterprise JavaBeans development

  • Print
  • Feedback

Page 3 of 6

Figure 3. A schematic description of testing EJB components by embedding OpenEJB as the EJB container. Click on thumbnail for full-sized image.

Create XML-mocks (optional)

To speed up testing, we can use XStream to create XML-mocks as input data for the tests. To use XStream, make sure the following jar files are in your classpath:

 xpp3-1.1.3.4d_b4_min.jar
xstream-1.0.2.jar 


The following statements create an XML representation of any Java object:

 XStream xstream = new XStream();
String xml = xstream.toXML(someJavaObject)); 


Let's see how we can use the above code to generate input data for your tests. Suppose you want to test the following code:

 public class CarReservation(){

public boolean reserveCar(Car car){ // Do something with the car } }


The reserveCar() method takes a car as parameter. Now, the question is how to create the Car object? Of course, one method would hard-code the car object. Another method would capture a snapshot of a Car object and reuse the snapshot as input for the tests. In our solution, we use the latter method. Suppose your application contains the following code:

 public void getCarInformationFromUser(Request request){
   Car car = request.getCar();
} 


You could modify that code to the following to get a snapshot of the Car object:

 public void getCarInformationFromUser(Request request){
   Car car = request.getCar();
   XStream xstream = new XStream();
   System.out.println("car=" + xstream.toXML(car)); 
}

If you run your application once, you can copy the XML representation of the Car object from the standard output and save it to a file.

Now you can test the reserveCar() method with the XML-mocks you just created:

 public void testReserveCar(){



/* * Read the XML string from a file */ String xml = SomeFileReader.getCarXML();

XStream xstream = new XStream(); Car mockCar = (Car) xstream.fromXML(xml);

/* * Continue the test */ CarReservation reservation = new CarReservation(); boolean ok = reservation.reserveCar(mockCar); assertTrue(ok);

}


You may decide to standardize the way objects convert to XML by creating an interface and letting your classes (or preferably superclasses) implement it:

 public interface XMLStylate {

/** * @return a string that contains the XML * representation of an object */ String toXML(); }


Your CarReservation class can be extended with the following method:

 public class CarReservation() implements XMLStylate{
   public boolean reserveCar(Car car){
   // Do something with the car
   }

public String toXML(){ XStream xstream = new XStream(); return xstream.toXML(this); } }


As you can see, the CarReservation provides a standard way of converting itself to XML.

For quality assurance, a piece of code must be tested under various scenarios, with each scenario having a set of input data. XML combined with XStream greatly simplifies the latter task.

Write your JUnit tests

Your EJB tests will look like code that is run within an EJB container, with no framework-specific code. Let's write a simple test for our Car Reservation application:

  • Print
  • Feedback

Resources