Overview

With Unit Test Assistant you can easily create multiple test cases for methods from a selected class or items selected in the Project Explorer by choosing the Add test case(s) option in the Unit Test Assistant view or in your IDE context menu. The methods are analyzed to create runnable test cases with initial values and sample assertions, which can be easily modified or uncommented.

Minimum RAM Requirement

UTA requires 2 GB of RAM for bulk creation. If you create multiple test cases for a large code base, we recommend configuring your IDE to use 4 GB of RAM.

Bulk Creation in IntelliJ IDEA

We recommend that you disable the Synchronize files on frame or editor tab activation option in the IntelliJ settings. If enabled, UTA may occasionally abort and then resume the test creation process, which may result in failing to generate one test, or generating a test that is incomplete. To disable the option:

  1. Go to File> Settings in your IntelliJ IDEA.
  2. Select Appearance & Behavior> System Settings.
  3. Disable the Synchronize files on frame or editor tab activation option.

Private methods

UTA creates test for private methods in the selected classes if the Create tests for private methods option in the UTA Preferences is enabled (see Configuring Preferences). By default, the Create tests for private methods option is disabled, since the industry best practice is to test private methods by creating tests for the accessible methods that call them, rather than by creating tests for private methods directly.

Creating Multiple Tests for One File

  1. Open the Unit Test Assistant - Add test case(s) dialog in one of the following ways:
    - Select a class in the editor and click the Add test case(s) action link in the Unit Test Assistant view:

      
    - Right-click a file in Project Explorer and select Unit Testing> Add test case(s) from the context menu:
      

    Spring Tests

    If you are creating multiple tests for Spring components, the button label will change into Add Spring test case(s). See Creating a Spring Unit Test for more information about creating and configuring Spring unit tests with UTA.

  2.  Specify the test type:
    - Regular - Creates regular test cases.
    - Parameterized - Creates parameterized test cases (see Creating a Parameterized Unit Test for details).
    - Combined - Creates parameterized test cases for methods that can be tested against different arguments. For other methods, regular tests are created.


  3. Specify the object initialization mode:
    Deep - (default) Prepares objects and mocks using data collected from the tested method and other methods that are called when the test is executed.
    Shallow - Prepares objects and mocks using data collected from the tested method.
  4. Specify how you want mocks to be initialized:
    - Configure within test method - Initializes mocks by declaring and initializing the object under test and its dependencies within the test method.
    - Use @InjectMocks - Initializes mocks by using the @InjectMocks annotation to mark fields on which mock injection should be performed and the @Mock annotation for dependencies.
    See Configuring Mock Initialization for details.
      

  5. (Optional) Enable or disable the Timeout for running created tests (in ms) option and specify the time limit (in milliseconds) for executing this set of test. If the time limit is exceeded, the test execution will stop and the test will fail. The information about the error will be displayed as a Recommendation in the UTA interface. This option is enabled and set to 1000 by default.
  6. Select the methods for which you want to create test cases (you can use the Select All or Deselect All buttons for your convenience).

    Spring Tests

    If UTA detects Spring classes in the selected resources, it will display additional options:

    Use Spring test framework within tests for Spring classes -allows you to specify the kind of tests you want to create for Spring classes: if enabled, UTA will create the tests using the Spring test framework (see Spring MVC Test Framework for details); if disabled, UTA will create standard JUnit tests.

    Select Spring Handler Methods - allows you to select all the Spring controller handler methods and deselect all the other methods in the list.


    See Creating a Spring Unit Test for details about support for the Spring framework.

  7. Click OK to create test cases.
    (info) UTA will add new test cases for all selected methods in the file (including methods that already have corresponding tests).
  8. View the tests to modify or uncomment the default UTA values if necessary.



  9. Run the test with UTA to collect coverage information and recommendations for possible improvements (see Executing Unit Tests with Unit Test Assistant).

Creating Multiple Tests for More Than One File

You can create test cases for more than one file.

  1. Right-click the files, package(s), or project(s) in your IDE.
    (info) You can also create test cases for individual methods selected in the Package Explorer if they are of the same class. If you select methods from different classes, or an individual method and a class or a package, test cases will not be generated.

  2. Click Unit Testing> Add test case(s) to open the the Unit Test Assistant - Add test case(s) dialog.

     

  3. Configure the options as described in steps 2-5 in the Creating Multiple Tests for One File section.
  4. (Optional) Hover over the License usage summary option to display an estimated number of tokens required to create test cases for the selected resources. Once the test are generated, the summary dialog will show how many tokens were actually consumed, and how many remain (see Licensing).



  5. Click OK to create cases. UTA will skip the files that are not testable or whose corresponding test classes already exist.

Licensing

Creating test cases for more than one file requires the 'Bulk creation' license feature. The feature provides license tokens that monitor the number of files for which test cases are generated  (the number of tokens depends on your license agreement). When creating test cases for multiple files, UTA consumes one token per .java file you selected.

(info) Files for which UTA does not generate any test cases (for example, interfaces or abstract classes) do not consume a token.

(info) Once a token has been consumed for a given java file, regenerating tests for that java file doesn't consume another token, even if the first test is deleted.

Recommended Workflow

To streamline test creation, we recommend that you leverage UTA's capability to automatically update multiple test with available assertions.

  1. Disable the Generate sample assertions option in the UTA preferences (see Configuring Preferences).
  2. Create multiple tests for methods from a file, package, or project (see Creating Multiple Tests from the Context Menu).
  3. Update the tests with correct input values.
  4. Run the tests to ensure they pass.
  5. Right-click the same file, package, or project that you selected in Step 2.
  6. Choose Unit Testing> Track and make assertions from the context menu.

See  Tracking Object Changes and Creating Assertions for details.

  • No labels