Chapter 15. Unit testing 499
Figure 15-1 Organization of test cases and test suites within a PiggyBank project
All tests of a package
We create an AllTests test case class in each package containing test cases.
This class defines a suite method that creates and returns a TestSuite
comprising all of the test cases in the package.
All tests of a project
We can also define an AllTests test case class in each project. This class
defines a suite method to collect all AllTests suites of a project into a single test
We can use this test organization to select the tests we want to run, whether a
single test case, or all of the test cases for a package, a project, or the entire
PiggyBank application.
Creating test cases
Application Developer contains wizards to help you build JUnit test cases and
test suites. We use this wizard to create the BusinessDelegateFactoryTest test
class to test the BusinessDelegateFactory class, which is used to create a
PiggyBank business delegate factory instance.
For more information about the BusinessDelegateFactory, see “Creating the
BusinessDelegateFactory” on page 266. The BusinessDelegateFactory is
defined in the ItsoAdFramework project. Be sure that your workspace contains
src folder with application
򐂰 application class (Xxxx)
junit folder with test case
򐂰 test suite (AllTests)
򐂰 test cases (XxxxTest)
Application package and test
package have the same name
500 WebSphere Version 5 Application Development Handbook
the ItsoAdFramework. For more information on how to import projects, see
“Importing a sample project into Application Developer” on page 669.
Preparing for JUnit
To use JUnit in the Application Developer Workbench, you have to add the JUnit
packages to the classpath of the Java project. Open the ItsoAdFramework project
򐂰 On the Libraries page, click Add JARs. Browse to the location of your
junit.jar file, the default location is ItsoAdLibs\junit-3.8.1, and click OK
(Figure 15-2).
Figure 15-2 Updating Java build path with JUnit
򐂰 Finally, we create an additional source folder named junit to capture the
ItsoAdFramework test classes. Select the source page and click Add Folder.
The Source Folder Selection window opens.
򐂰 Click Create New Folder and type junit for the Folder Name.
򐂰 Click OK to accept the Folder Name. Click OK and the junit is added to your
Source folders list (Figure 15-3).
Chapter 15. Unit testing 501
Figure 15-3 Updating source folders
򐂰 Click OK to accept all the changes.
Creating a test case
We create a test case for the createPiggyBankBusinessDelegate method of the
BusinessDelegateFactory class:
򐂰 To begin, use the Java perspective and expand the ItsoAdFramework project,
src folder, and its packages. In the package,
select the BusinessDelegateFactory class and New -> Other -> Java -> JUnit
-> TestCase and click Next.
򐂰 Figure 15-4 shows the JUnit TestCase wizard, where you enter details about
the new test case:
Set the source folder to ItsoAdFramework/junit (our test case folder).
–Select setUp() to create a stub for this method in the generated file.
We leave the default values for all other fields on this page:
Test case—The name of the new test case class is generated as the name
of the source class we want to test, with Test as a suffix.
Test class—The Java class that this new test case is testing.
Superclass—By default, the JUnit TestCase class, but can be changed if
you extend the JUnit package yourself.
Method stubs—Which method stubs to generate.
502 WebSphere Version 5 Application Development Handbook
Figure 15-4 Specify test case and test class
򐂰 Click Next to proceed to the last page of the wizard. Figure 15-5 shows the
available methods for which stubs should be created. We select the
createPiggyBankBusinessDelegate method.
Figure 15-5 Select test methods

Get WebSphere Version 5 Application Development Handbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.