Test data tables
A test data table allows you to define test input and output data that can be used when you run a test. Test data tables are synchronized with the test behavior script, so modifications that you make to the code are reflected in the test data table and vice versa.
Because each test data table maps to a particular test case (a JUnit test method) in your test code, a separate test data table is created for each test method. As you work with the test behavior code, the test data table view updates automatically when you move the cursor from one test method to another. When you first open your test behavior code, no test data table is displayed until you move the cursor to a test method in the code.
Each row in the test data table represents an object or expression in your code, and each column represents a data set (equivalence class). 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.
Test data tables provide automatic syntax and data type validation to guarantee that the data you enter is syntactically correct.
Note: Syntax validation is turned off automatically if a test does not compile.
With test data tables you can perform the following tasks:
- Supply test data in the form of expressions, sets, and ranges
- Add elements to an array
- Supply test data for the attributes of object data types
- Partition your data into data sets (equivalence classes)
- Use subclasses to initialize and test an object
Supplying test data in the form of expressions, sets, and ranges
Any syntactically valid expression that can appear on the right side of an assignment statement can be entered as an expression in the test data table. This includes primitives, strings (enclosed in double quotes), variable references, and constructor and method calls
You can also insert sets and ranges to be used as test input, expected outputs, and return values. The test data table contains its own set and range editors for defining sets and ranges. Simply right-click in a table cell and click Define Set or Define Range. Note that you cannot enter a set or range within the cell without using the set or range editors. A set is shown in a data table by curly braces ({ }) and by as many comma-separated values as required to define the set. A range is shown in a data table by comma-separated values enclosed within square brackets ([ ])
Adding elements to an array
In addition to using expressions, ranges, sets, and objects in test data tables, you can also use arrays. Arrays are containers for an ordered-list of primitive or object elements. Arrays can be one-dimensional or multi-dimensional, that is, they can contain other arrays. Arrays are displayed hierarchically in the test data table with each element in the array appearing in its own row, nested beneath the row that represents the array. Each element in the array is displayed with its index and can be retrieved and changed independently. Thus, the first element in an array is displayed as [0], the second element is displayed as [1], and so on.
Supplying test data for the attributes of object data types
Arguments that are based on primitive types (int, char, double, long, and so on), wrapped primitive types (for example Integer or Double), or Strings can be visualized as a single row in a test data table. However, objects with multiple defining attributes cannot be visualized in a single row in the test data table. These complex objects are represented hierarchically in several rows of the test data table.
When you click in the test data table in any cell that contains an object, you will be able to select from a list of ways to define the object. Objects can be defined according to one or several constructors or one or several sets of object properties. When you choose an item from the list, the row representing the object expands to display the defining attributes that you have chosen
Note: Hashmaps (java.util.HashMap) are not supported in test data tables.
Partitioning your data into data sets
A data set (or equivalence class) is a set of input values that are all expected to invoke the same behavior. Data sets help you partition your data into logical units that should be handled the same way by your code. Each data set in a test data table contains one column for the input values and another column for the expected output values.
Each data set provides values for one or several test runs. If there are three data sets in the test data table, the same test behavior will be executed at least three times. (If sets and ranges are used as values in the test data table, there will be a test run for each value in the set or range.)
Using a subclass to initialize and test an object
Test data tables contain a type column that can be edited so that variables of an interface type or a type with inherited classes can have data that is of the implementing class or subclass. For example, take a hypothetical method named drawCalendar that takes a single argument of type Calendar, which in turn has GregorianCalendar and LunarCalendar as subclasses. If you want to define a set of tests that pass an object of type GregorianCalendar, you would edit the argument's type field. When you edit the type, the original argument type is displayed in the method signature.
Related concepts
Data sets
Expressions
Test-generation process
Stub data tables
Related tasks
Defining sets
Defining ranges
Adding elements to an array
Supplying test data for the attributes of object data types
Using a subclass to initialize and test an object
Selecting an expected exception
Defining test data