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:
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.
To view a test data table:
Here is a short list of some of the actions you can perform with a test data table:
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.
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:
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.