Unit Testing in JBuilder by Charlie Calvert

By: Charlie Calvert

Abstract: This is part I of a three part article about a technology you can use in JBuilder called unit testing. Unit testing helps you create Boolean tests for many features of your code.

Unit Testing with JUnit

Copyright © 2002 by Charlie Calvert

This is part I (one) of a three part article about a technology you can use in JBuilder called unit testing. Unit testing helps you create Boolean tests for many features of your code. Using a tool called JUnit , these methods will be called automatically, and a report generated to confirm that they all passed correctly, or that one or more of the tests failed. Tests that return true succeed, those that return false, fail.

Unit testing is a way of life. It is part of a philosophy of development called extreme programming . One of the core concepts of extreme programming is that you should always be producing code that compiles and runs correctly. Even if you only have one feature completed out of project that will include hundreds of features, you should still start with a version of your product that fully supports that one feature.

To confirm that you product is working correctly, you should write a series of Boolean tests that demonstrate the correctness of the code you have written. As you begin to add more code to your project, you will continue to add tests and to write new tests. You should run the tests at least once a day, preferably many times each day. They will prove not only that your new features work, but that the new features do not break any of your already established code base.

To help make this process as simple as possible, an open source project call JUnit was created. The sole purpose of JUnit is to make it as easy as possible for you to write and run tests of the kind described in the last few paragraphs.

NOTE: Most of the features for integrating JUnit into JBuilder are included only in the enterprise version of JBuilder. However, JUnit is an open source project. So don't fear, you can and should use JUnit regardless of which version of JBuilder you are using. I will show you first how to integrate the tools into JBuilder enterprise, and then spend at least a little time showing how to proceed even if you are working with JBuilder personal. Everyone ought to use JUnit, not just those with the enterprise edition. You might want to read the text through from the beginning regardless of which edition you are using. There are many hints about using JUnit scattered throughout this article.

NOTE: Unit code is particular powerful when used in combination with another open source tool called Ant. Ant bears more or less the same relationship to Java that the make utility does to C/C++. The big differences between Ant and make is that Ant is easily extensible and its XML based syntax is relatively easy to understand. I will not discuss Ant in this article, but I do discuss it in my upcoming book, Learn JBuilder 7.0, from which this article is excerpted.

Unit Testing in the Enterprise Version

In this section of the text I will show you how to use JUnit from the enterprise version of JBuilder. Later in the text I will show you how to proceed if you have the personal edition. I will try to add at least a few notes to this section to help people along who do not have the Enterprise version of the product. The code shown in this chapter is drawn mostly from an example program called UnitTesting, found on the CD that accompanies this book.

NOTE: The code generated by JBuilder enterprise is simply Java source code for use with the open source project called JUnit. Therefore you can download JUnit from www.junit.org, and then use it to run the scripts described in this chapter. The scripts themselves are easy to understand, and you should have no problem generating them by hand. The JUnit site also includes code for automatically generating tests, so you can use the open source code in lieu of the code in the enterprise version of JBuilder. In short, this chapter really is for everyone, and not just users of the enterprise edition of JBuilder.

Start a new application using the techniques described in Chapter 3, "Default Applications and Applets." Add the following two methods to the Frame1.java:

        public int add(int a, int b)
        {
                return a + b;
        }


        public int multiply(int a, int b)
        {
                return a * b;
        }

Don't bother to call the methods. For now, all that matters is that the two methods are included in Frame1.java.

Make sure Frame1 is focused in the Content Pane. Now choose File | New from the JBuilder menu and turn to the Test page in the Object Gallery. Click on the Test Case option. The dialog shown in Figure 1 will appear. Highlight the two public methods that you see in the dialog, and press the Finish button. Don't bother stepping through the whole wizard, just press finish after filling out the first page.

Figure 1: Selecting the add and multiply methods from Frame1 so that you can test them with JUnit.

After you press Finish, the code shown in Listing 1 will be generated automatically. This is the test code used to confirm that the functions we have written work correctly. Notice the presence of the import junit.framework.* statement at the top of the source file. Notice further that TestFrame1 extends a class from the JUnit framework called TestCase. You are extending this class solely so that you can add the functionality for testing your two methods to it. All the other features necessary to automatically call your methods, and to give you a graphical report on the success of you methods, is built into JUnit itself.

Listing 1: A class generated by JBuilder and designed to automatically test the methods found in Frame1. You need only add the assert statements to this code.

package unittesting;

import junit.framework.*;
import java.awt.event.*;


import unittesting.*;


public class TestFrame1 extends TestCase
{
  public TestFrame1(String s)
  {
    super(s);
  }


  protected void setUp()
  {
  }


  protected void tearDown()
  {
  }


  public void testAdd() 
  { 
    Frame1 frame1 = new Frame1();
    int a1=  2; 
    int b2=  2;
         int intRet = frame1.add(a1, b2);
  }
  
  public void testMultiply() 
  { 
    Frame1 frame1 = new Frame1();
    int a1=  2;
    int b2=  3;
    int intRet = frame1.multiply(a1, b2);
  }
}

The code for setting up and tearing down the class is not needed in our case, so you can delete it or ignore it. You should also add code for actually performing tests in the testAdd and testMultiply methods, as described immediately after the listing. The end result is the class shown in Listing 2.

Listing 2: A valid test case for the Frame1 class.

package unittesting;

import junit.framework.*;


import unittesting.*;

public class TestFrame1 extends TestCase
{
  public TestFrame1(String s)
  {
        super(s);
  }

  public void testAdd()
  {
        Frame1 frame1 = new Frame1();
        int a1=  2;
        int b2=  2;
        int intRet = frame1.add(a1, b2);
        assertEquals( intRet, 4);
  }

  public void testMultiply()
  {
        Frame1 frame1 = new Frame1();
        int a1=  2;
        int b2=  3;
        int intRet = frame1.multiply(a1, b2);
        assertEquals( intRet, 6);
  }
}

Take a look at the testAdd method. It uses code generated by JBuilder to set up the test. Then a method called assertEquals is called. This method tests to make sure that the return value from the Frame1.add method is equal to 4. If it is equal to 4, the test succeeds. If it is not equal to 4, the test fails. The same principle is applied to the testMultiply method.

Now that you have your test case set up, the next step is to run the test case and see if it works. Perhaps the simplest way to do that is to pick TestFrame1.java out of the list in the project pane. Right click on it and choose run. You do not want to run your main application, instead you want to run the test case that was generated by the JBuilder wizard. The results of a successful run are shown in Figure 2.

Figure 2: A successful JUnit test run in the JBuilder IDE. Notice the checkmarks in the round circles shown in the message pane.

The JBuilder IDE gives you visual feedback so you can verify the results of these tests. Figure 2 shows the case where both tests succeed, Figure 3 shows the case where the second test fails. The failures and success are clearly visible on a color screen. Successful calls are shown in green, failures in bright red.

The second test might fail if you changed the testMultiply method to look like this:

  public void testMultiply()
  {
        Frame1 frame1 = new Frame1();
        int a1=  3;
        int b2=  3;
        int intRet = frame1.multiply(a1, b2);
        assertEquals( intRet, 6);
  }

This test will fail because 3 times 3 does not equal 6. The call to assertEquals states that the variable intRet should be equal to 6. If it is equal to 6, then the call succeeds, otherwise an error state is recorded by JUnit.

Figure 3: A failed JUnit test run in the JBuilder IDE. Notice the X marks found in the round circles shown in the message pane. These will show up in red on your screen.

assertEquals is one of series of methods supplied by the developers of JUnit for testing your code. The other methods are called assertNotNull, assertNull, assertSame and assertTrue. There is also a method called assert, but it has been deprecated now that the JDK has a method of the same name built into it. The method called assertTrue was written to replace the old JUnit call to assert.

NOTE: This is the end of Part I. Part II and Part III are both available.


Server Response from: ETNASC04