Skip to main content

skip to main content

developerWorks  >  WebSphere  >

Integrating the JUnit Plug-In with WebSphere Studio

developerWorks
Document options

Document options requiring JavaScript are not displayed


Rate this page

Help us improve this content


Level: Intermediate

Sunil Mahamuni (msunil@in.ibm.com), Senior Software Engineer , IBM Global Services India
Pagadala Suresh (pjsuresh@in.ibm.com), Software Engineer, IBM Global Services India

10 Mar 2003

This article shows you how to integrate the JUnit plug-in into WebSphere Studio so that you can unit test the code and view test results using JUnit.

Introduction

Junit is an open-source, industry-proven JavaTM unit-testing framework. IBM® WebSphere® Studio Application Developer (hereafter called Application Developer) is a next-generation IDE based on the open-standard Eclipse architecture. This article explains how to integrate the JUnit Plug in with Application Developer and test a sample class in Application Developer using JUnit.



Back to top


Prerequisites

Hardware

  • 500 Mhz Pentium P3 processor
  • 256 MB RAM

Software

  • Windows NT® 4.0 or Windows® 2000
  • WebSphere Studio Application Developer Version 4.0 (Versions 4.01 and 4.02 can be used, but the appearance of some screen images may differ slightly from the ones shown below).

Skill level

You should know how to do simple tasks in Application Developer such as opening a perspective or creating a Java class.



Back to top


Integrating JUnit into Application Developer

  1. Download the following files from eclipse.org:
    • org.junit.zip (Use org.junit) It contains the plug-in for the JUnit testing framework.
    • org.eclipse.jdt.junit1.0.0.zip (Use org.eclipse.jdt.junit) It contains the plug-in for launching JUnit tests from Eclipse and for viewing the test results.
  2. Extract the above files into the plugins directory of Application Developer: <Application Developer>\plugins
  3. Open the Application Developer workbench.
  4. Select Window => Preferences.
  5. In the Preferences window, select Plug-in development.
  6. The right pane displays all loaded plug-ins. Click Reload to load the JUnit Plug-in.
  7. After reloading, find the following items in the loaded Plug-ins list:
  8. Restart Application Developer.
  9. Select Perspective => Show view => Other => Java.
  10. Under the Java perspective, find the JUnit view.


Back to top


Running a test case

1. Setting up the project to run test cases

  1. Open the Java perspective.
  2. Create a Java project named SampleJavaProject.
  3. In the context menu of the project, select Properties.
  4. In Properties window of Java, select Java build path.
  5. In the right pane, select the Libraries tab.
  6. Click Add External JAR.
  7. Select junit.jar from the <Application Developer>\plugins\org.junit\ directory.
  8. To look at the JUnit source code, add src.jar from the above directory.
  9. After adding junit.jar and src.jar, add them to the libraries, as in the highlighted portion of Figure 1 below.
  10. In the Properties window of the selected project, click OK.

Figure 2. Java build path
Figure 2

2. Creating a sample class

  1. Open the Java perspective.
  2. Create a Java class named SampleJavaClass in the package test.sample in the SampleJavaProject created above.
  3. Open SampleJavaClass in the Java Editor, add the code below, and save it.

    Listing 1. package test.sample

    package test.sample;
    public class SampleJavaClass {
    
       /**
       * Constructor
       */
    
       public SampleJavaClass() {
           super();
       }
    
       /**
       * Adds two numbers. This method is tested
       * by sample test cases.
       */
       public int add(int a, int b) {
           return a + b;
       }
    }
    
    


  4. SampleJavaClass contains the method add(), which is tested by the sample test case.

3. Creating the sample test case

  1. Open the Java perspective.
  2. Create a Java class named SampleJavaTestCase in package test.testcase in SampleJavaProject.
  3. Open SampleJavaTestCase in the Java Editor, add the code below, and save it.

    Listing 2. package test.testcase

    package test.testcase;
    
    import junit.framework.TestCase;
    importtest.sample.SampleJavaClass;
    
    public class SampleJavaTestCase extends TestCase {
    
        /**
         * Constructor for SampleJavaTestCase
         */
        public SampleJavaTestCase(String name) {
           super(name);
        }
    
        /**
         * Test to test the method add in Sample Java Class
         */
        public void testAdd() {
            SampleJavaClass samp = new SampleJavaClass();
            int c = samp.add(2, 3);
            assertEquals(c, 5);
        }
    
        /**
         * Test to test the method add in Sample Java Class.
         * In this test, wrong expected value is given in
         * assert statement, assertEquals(c, 10), to demonstrate assert statement.
         */
        public void testAdd1() {
            SampleJavaClass samp = new SampleJavaClass();
            int c = samp.add(2, 3);
            assertEquals(c, 10);
        }
    
        /**
         * Main method to run Test Case in JUnit
         * Frame Work
         */
        public static void main(String[] args) {
            junit.textui.TestRunner.run(new SampleJavaTestCase(""));
        }
    }
    


  4. The assertEquals Statement is used to verify the result with the expected output:
    assertEquals(actualOutput, expectedOutput);
    In the output, it will list the test under failures if actualOutput and expectedOutput do not match.
  5. The above test case contains two tests of the add() method of SampleJavaClass. testAdd() demonstrates the output of a successful test, and testAdd1() demonstrates the output of a failed test.

4. Running the Test Case

  1. Open the Java perspective.
  2. Make sure that the following classes compiled properly:
    • test.sample.SampleJavaClass
    • test.testcase.SampleJavaTestCase
  3. In the Packages view, select the test case SampleJavaTestCase.
  4. In the toolbar, select Run => Junit tests.
  5. If the Debug perspective opens, go back to the Java perspective.
  6. The JUnit view opens with the test case results.
    Hint: Turn off the option Window => Preferences => Debug => Show Debug perspective when a program is launched in Run mode so that it won't show the Debug perspective after executing the test case.



Back to top


About JUnit view

JUnit view is invoked when you run a test case. It indicates the failure of a test by a red status bar, as shown in Figure 3. JUnit view shows its results under the two tabs: the Failures tab and the Hierarchy tab: tabs

Figure 3 depicts the user interface presented by JUnit view under the Failures tab. It lists all failed tests and their respective failure traces.


Figure 3. Failures tab
Figure 3. Failures tab
Figure 4 depicts the user interface presented by JUnit view under the Hierarchy tab. It lists all test cases and their status of execution. A green tick mark before the test indicates it succeeded, while an "x" () indicates that the test failed:tick markx mark
Figure 4. Hierarchy tab
Figure 4. Hierarchy tab

The JUnit view shows a green status bar if all the tests are successful, as shown in Figure 5:


Figure 5. Successful execution
Figure 5. Successful execution

In addition to the above information, JUnit View provides the following test results:

  • Number of tests run out of total number of tests -- Specifies number of tests executed against the total number of tests submitted for execution.
  • Number of Errors -- Specifies total number of errors encountered during execution of all tests.
  • Number of Failures -- Specifies total number of failures during execution of assert statements.
  • Status bar of Application Developer -- Specifies time taken to finish the tests.

Failures are anticipated and checked for with assertions while errors are unanticipated.



Back to top


About JUnit Test Finder

JUnit Test Finder is used to search for test cases or test suites in a given package or project. You can select any test case or test suite to run from the list provided by the JUnit Test Finder:


Figure 6. JUnit test finder
Figure 6. JUnit test finder

Searching for test cases or test suites in a package or project

  1. Select project or package to find for the test cases.
  2. Click on JUnit tests, as shown in Figure 8.
  3. The JUnit test finder window opens with a list of test cases or test suites under the project or package (see Figure 6).
  4. Select the required test case and click Finish.

Relaunch last

To relaunch the last run test case, select Debug => Relaunch last, or press the F10 key.



Back to top


Filter patterns

Filter patterns are used to filter out uninteresting stack frames from a test failure notice.


Figure 7. Stack filter pattern
Figure 7. Stack filter pattern

Defining filter patterns

  1. From the Application Developer tool bar, select Window => Preferences.
  2. Select JUnit.
  3. The right pane displays a list of stack filter patterns. Figure 7 depicts the list of filter patterns and a dialog box to enter new filter patterns.
  4. To add patterns to the list, click Add.
  5. To delete patterns from list, click Remove to filter our uninteresting stack frames in the failure notice.

Check Filter stack trace entries failed tests to filter the added stack filter patterns. Uncheck Filter stack trace entries failed tests to disable the filtering mechanism.



Back to top


Test case modes

A test case can be launched in either of the following two modes:


Figure 8. Launching a test case in Run mode
Figure 8. Launching a test case in Run mode

Figure 9. Launching a test case in Debug mode
Figure 9. Launching a test case in Debug mode

In the previous example, the test case was launched in Run mode, as shown in Figure 8. To see the details of a failed test case, launch it in Debug mode, as shown in Figure 9.

Launching a test case or test suite in Debug mode:

  1. Select the test case or test suite in Package Explorer.
  2. Click the Debug icon Debug icon in the menu bar for drop-down menu.
  3. Expand the Debug menu item and click JUnit tests. The Debug perspective will be opened by suspending control at a breakpoint.


Back to top


Conclusion

This article has shown you how to:

  • Integrate JUnit with Application Developer and automate unit testing.
  • Find test results in JUnit view.
  • Use JUnitTestFinder to search the test cases in a particular project or package.
  • Make stack trace information clear by filtering uninteresting stack frames out of the stack trace.
  • Run test cases in debug mode to debug the class being tested.


Resources



About the authors

Sunil Mahamuni is a Senior Software Engineer and Team Leader in IBM Global Services India. He works in the Financial services sector on Banking and Insurance applications. His past experience includes J2EE and wireless development projects. He has done his Master of Computer Application from Osmania University -INDIA. u can reach Sunil at msunil@in.ibm.com .


Photo of Pagadala Suresh

Pagadala J. Suresh is a software engineer in IBM Global Services, Inida. He did his Masters degree in Computer Applications in Karnataka Regional Engineering College, Surathkal and Batchelors degree from Akkineni Nageshwara Rao College, Gudivada.. He is IBM Certified Application Developer Associate for Websphere Studio Application Developer. He worked on static security of exposures in Internet Applications. His expertise is on WebSphere and Java Technologies. His domain skills on Banking and Insurance. He has hands on experience on IBM WebSphere Business Components Composer Framework. You can contact him at pjsuresh@in.ibm.com .




Rate this page


Please take a moment to complete this form to help us better serve you.



 


 


Not
useful
Extremely
useful
 


Share this....

digg Digg this story del.icio.us del.icio.us Slashdot Slashdot it!



Back to top