Exercise 1.3: Editing the test

Before you begin, you must complete Exercise 1.2: Creating a Java Component Test

After creating a test, you can edit it to make it do exactly what you want. For example, you can:

Defining test data

In addition to the test behavior code, test suite, and one or several test cases, a test contains test data in the form of one or more test data tables. When you create a test, one test data table is created for each test case. You can use the test data tables to perform data-driven testing.

Each row in a test data table represents an object or expression in your code, and each column pair represents a data set (or equivalence class, as it is sometimes called). Each data set column is divided in half, one half for the input values, and the other half for the expected output values. Expected outputs include return values, exceptions, and any parameters that are specifically marked for output. Return values and exceptions are mutually exclusive.

Any syntactically valid expression that can appear on the right side of an assignment statement can be entered in a test data table cell.  Thus, all of the following are considered to be valid: primitive values, strings (enclosed in double quotes), variable references, and constructor and method calls. Expressions can also include variables, constants, and logical operators.

The following sample test data table shows a cell with a range of values, another cell with a single integer value, and another cell with an expected exception.

Test data table example

To view a test data table:

  1. Open the test behavior code that is generated when you create a test.
  2. Click any test method.

Here is a short list of some of the actions you can perform with a test data table:

Editing the test behavior code

In addition to editing the code by hand, you can simply right-click in the Java editor and click Component Test to display a menu of elements you can add to the code. The test behavior code is synchronized with the test data tables, so modifications that you make to the code are reflected in the test data table and vice versa.

Creating stubs and stub data

For testing purposes, you might find it useful to stub out classes that the code you are testing interacts with. Stubs are typically used for the following purposes:

When you create a stub,  you can reuse it in multiple tests.

To create a stub, you use a wizard similar to the one you use when you create a test. To stub a class:

  1. Click File > New > Other > Component Test > Java > Java Component Stub.
  2. Select the test project that will contain the stub and click Next or click New to create a new project and click Next.
  3. Select the Java source files or libraries that you wish to stub and click Finish. This displays the Test Suite editor.
  4. In the Test Suite editor, click the Stubs tab.
  5. Click Add, select the stub you want to add to the test suite, and click Finish.
  6. Still in the Test Suite editor, click the Overview tab and under Stubs, click the stub that is defined in the test suite. This displays the Stub page of the Test Suite editor.
  7. Under General Information, click Edit next to the name of the stub's Java source file. This displays the stub user code.
  8. Click on a method in the stub code to display the stub data table.
  9. Enter your stub data.

Note that stub data tables work somewhat differently than test data tables. With the stub data table, you simulate the stubbed class by specifying the actual input and return values for each stubbed method.

To replace the stub with the real class at any time, simply remove the stub from the test suite, using the Test Suite editor.

Now you are ready to begin Exercise 1.4: Running the test and analyzing the result.

Terms of use | Feedback
(C) Copyright IBM Corporation 2000, 2005. All Rights Reserved.