 | 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.
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.
Integrating JUnit into Application Developer - 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.
- Extract the above files into the
plugins directory of Application Developer:
<Application Developer>\plugins - Open the Application Developer workbench.
- Select Window => Preferences.
- In the Preferences window, select Plug-in development.
- The right pane displays all loaded plug-ins. Click Reload to load the JUnit Plug-in.
- After reloading, find the following items in the loaded Plug-ins list:
- Restart Application Developer.
- Select Perspective => Show view => Other => Java.
- Under the Java perspective, find the JUnit view.
Running a test case 1. Setting up the project to run test cases - Open the Java perspective.
- Create a Java project named
SampleJavaProject. - In the context menu of the project, select Properties.
- In Properties window of Java, select Java build path.
- In the right pane, select the Libraries tab.
- Click Add External JAR.
- Select junit.jar from the
<Application Developer>\plugins\org.junit\ directory. - To look at the JUnit source code, add src.jar from the above directory.
- After adding
junit.jar and src.jar, add them to the libraries,
as in the highlighted portion of Figure 1 below. - In the Properties window of the selected project, click OK.
Figure 2. Java build path

2. Creating a sample class - Open the Java perspective.
- Create a Java class named
SampleJavaClass in the package test.sample
in the SampleJavaProject created above. - 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;
}
}
|
SampleJavaClass contains the method add(), which is tested by the sample test case.
3. Creating the sample test case - Open the Java perspective.
- Create a Java class named
SampleJavaTestCase in package test.testcase in SampleJavaProject. - 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(""));
}
}
|
- 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. - 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 - Open the Java perspective.
- Make sure that the following classes compiled properly:
test.sample.SampleJavaClass
test.testcase.SampleJavaTestCase
- In the Packages view, select the test case SampleJavaTestCase.
- In the toolbar, select Run => Junit tests.
- If the Debug perspective opens, go back to the Java perspective.
- 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.
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:
 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 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:  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

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.
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

Searching for test cases or test suites in a package or project - Select project or package to find for the test cases.
- Click on JUnit tests, as shown in Figure 8.
- The JUnit test finder window opens with a list of test cases or test suites under the project or package (see Figure 6).
- 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.
Filter patterns Filter patterns are used to filter out uninteresting stack frames from a test failure notice.
Figure 7. Stack filter pattern

Defining filter patterns - From the Application Developer tool bar, select Window => Preferences.
- Select JUnit.
- 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.
- To add patterns to the list, click Add.
- 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.
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 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:
- Select the test case or test suite in Package Explorer.
- Click the Debug icon
in the menu bar for drop-down menu. - Expand the Debug menu item and click JUnit tests.
The Debug perspective will be opened by suspending control at a breakpoint.
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
.
|
 | 
|  |
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
|  |