Rate This Document
Findability
Accuracy
Completeness
Readability

Generating a UT Case

Prerequisites

Ensure that the project file can be compiled.

Procedure

UTgen can generate UT cases in the form of Java packages, files, and functions.

  1. In IntelliJ, expand the project folder, right-click a Java package or file and select Generate Tests with UTgen from the shortcut menu. Alternatively, open the file. Right-click the function for which you want to generate UT cases, and then select Generate from the shortcut menu. Among the options that are displayed, select Tests with UTgen.
    Figure 1 Generate Tests with UTgen
  2. UTgen automatically sets the UT parameters. See Table 1.
    Table 1 UT parameters

    Parameter

    Description

    Test sources root

    Directory of storing the code files of generated UT cases.

    Testing framework

    Select a test framework to generate UT cases.

    • JUnit4
    • JUnit5

    UT generation for same function

    When a UT case is added to the same function, the original UT case (with the @UTgen annotation) generated by the tool is overwritten or retained.

    • Overwrite
    • Retain

    Mocking strategy

    Indicates whether to mock internal objects.

    • Do not mock
    • Auto mock
    • Mock outside the class
    NOTE:
    • Mock static methods
    • Parameterized tests

    If you select Auto mock or Mock outside the class, you can select Mock static methods as well. If you select Do not mock, you can select Parameterized tests as well.

    Test generation timeout

    Timeout interval for generating a UT case for each class. The default value is 60s.

    Generate tests for

    Lists the tested functions or classes.

    In the list, you can select or deselect the functions or classes in the Java package or file.

  3. Click Generate Tests to generate UT cases. Alternatively, click Generate and Run to generate and run UT cases. A progress bar is displayed in the lower right corner.
    Figure 2 Generating test cases
    Figure 3 Progress bar

    When UT cases are already being generated, no more UT cases can be generated at the same time.

  4. After UT cases are generated, a success message is displayed in the lower right corner. In addition, a UT directory is generated in the root directory (specified by the Test sources root parameter) in the project folder. The UT case paths generated in the directory correspond to the paths in the project folder.
    Figure 4 Success
    Figure 5 Folder of generated UT cases

When generating UT cases, check that the language level of the source in File > Project Structure > Modules matches the module SDK in the dependency.