In this section:
You can extend the analysis performed by UTA with the tracking actions that monitor an object and compare its state before and after the execution of a method. This gives you insight into how particular values changed during execution and helps you make relevant assertions. The following tracking actions are displayed in the Unit Test Assistant view:
- Track [method_name] - monitors a method call and displays available assertions in the Recommendations view. You can manually update your test with recommended assertions by applying a quick-fix (action link); see Manually Creating Assertions.
- Track and make assertions - monitors a method call and automatically applies all recommended assertions that are not already present. This option is not available for parameterized tests and tests that fail to run.
Tracking Object Changes
- Create a new test with UTA or open an existing test.
- Click a test method. If the test method's name is consistent with the name of the method under test, UTA will automatically identify the tested method and display two tracking options: Track [method_name] that tracks the method call and Track and make assertions that tracks the method call and automatically creates assertions.
To track an invocation of a method whose name is different than the test method name, select a specific method invocation in the editor. UTA will display a Track [method_name] action for the automatically-detected method, as well as for the selected method. The Track and make assertions action will also be available so you can track and update the selected method.
- Click one of the Track action links to run the test and collect information about the values that changed during execution.
- View the information about object changes. The Variables tab displays information about values that were used during execution. The >> icon indicates values that changed.
- View the information about the available assertions in the Recommendations tab and add the assertions manually (see Manually Creating Assertions). This step is required if you do the tracking with the Track [method_name] action.
Manually Creating Assertions
You can make assertions from both the Recommendations and the Variables views.
- In the Recommendations view, click the Make assertions quick-fix.
- In the Variables view, right-click the value you want to assert and choose Make assertions.
Alternatively, you can copy the value with the Copy option to manually complete the code.
If the detected changes are in inaccessible fields, UTA generates assertion templates that require manual modifications to the code:
You can enable or disable generating recommendations for inaccessible fields; see Configuring Preferences for details.
Creating Assertions for Multiple Tests
You can track and automatically create assertions for more than one test.
To create assertions for tests from the same class:
- Open the test class.
- Click anywhere outside any test methods.
- Click the Track and make assertions action in the Unit Test Assistant view.
To create assertions for tests from different classes, packages, and/or projects:
- Select the desired classes, packages, and/or projects in the Project Explorer.
- Choose Unit Testing> Track and make assertions from the context menu.
Assertions Tips and Tricks
- If you create multiple tests with UTA, we recommend disabling the Generate sample assertions options in UTA preferences to avoid redundant assertions in the final tests (see Configuring Preferences and Creating Multiple Unit Tests).
- When using the Track and make assertions action, you may want to set the console verbosity level to High to view additional details about the assertion creation process (see Customizing Console Verbosity). The console will display the number of assertions created per test case and details about assertions that were not created.