Exercise 1.2: Creating a Java component test

Before you begin, you must complete Exercise 1.1: Creating a test project..

After you create a test project, you can use the Create Java Component Test wizard to create Java component tests and stubs. If you need to test a Java interface, abstract class, or superclass, you can also create a special kind of component test called an abstract test.

In this example, the wizard takes you through the following steps:

  1. Starting the wizard and selecting a test project
  2. Using the test guidance to decide what to test
  3. Selecting a test pattern
  4. Defining the test scenario

Starting the wizard

To start the wizard:

  1. Click File > New > Other > Component Test > Java > Java Component Test.
  2. Select the test project that will contain the test or click New to create a new project and click Next.

Using test guidance to help you decide what to test

After you select a test project, a static analysis is performed on the Java source files associated with the test project. These files were selected during the creation of the test project and serve to define the scope of the test. The list of files in the project can be updated by modifying the test project's Test Scope properties.

When the analysis is complete, you will see a list of components in a table format and sorted according to the computed metrics. You can use the guidance that these metrics provide to help you decide which classes or components are most important for you to test. Components with highlighted values or high numerical values are considered high-priority test candidates.

In the following figure, for example, GaussianIntegerRandomGenerator would be one place to start, so to test this class, simply click the check box next to GaussianIntegerRandomGenerator and click Next. (You can also click Options to modify which metrics are displayed and to change the sort order.)

Computed Metrics displayed in create test wizard

Selecting a test pattern

After you select the classes you are going to test, you need to select a test pattern. Test patterns provide a sort of template for different kinds of Java component tests. The available test patterns for Java components include:

For your first test, select the scenario-based test pattern and click Next.

Defining the test scenario

After you select the test pattern, you will see a wizard page that lets you define the test scenario. The process is as follows:

  1. Add an instance of the class-under-test by selecting a constructor from the list and clicking Add. (You can also double-click a constructor to add it to the scenario.)
  2. If you want to assign a more meaningful name to the class instance, select the instance from the test scenario, click Rename, enter the new name, and click OK.
  3. Click Show inherited methods to display all of the methods that can be included in the scenario.
  4. Double-click each method to be included in the scenario. (A sample scenario is shown below. The scenario has one constructor and four methods.)
  5. When you are finished building the scenario, click Finish.

Wizard page for adding constructors and methods to a test scenario

Viewing the results

When you create a scenario-based test, a single test case is created within the test suite. In the test behavior code, the test case is implemented as a single JUnit test method. After you finish creating the test, the Overview page of the Test Suite editor is displayed automatically, as shown below:

Test Suite editor overview page

From this page, you can edit the name of the test, add a description of the test, and open the test behavior code in the Java editor. To view the code, click the Behavior (/StatTest/Behavior/test/GaussianIntegerRandomGeneratorTest.java in the example above).

Test behavior code

Now you are ready to begin Exercise 1.3: Editing the test.

Feedback
(C) Copyright IBM Corporation 2000, 2005. All Rights Reserved.