Rate This Document
Findability
Accuracy
Completeness
Readability

Generating a UT Case

Test cases can be generated based on Spring Boot projects and common projects.

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 Based on common projects
    Figure 2 Based on the Spring Boot projects
  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.

    If some classes time out, the number of timeout classes is displayed in the corresponding source code folder, and a timeout message is displayed for these timeout classes.

    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.

    The three additional parameters are used for Spring Boot projects. See Table 2.

    Table 2 UT parameters

    Parameter

    Description

    Spring configuration

    You can select a Spring configuration class or choose not to set this parameter.

    • No configuration: No special processing is performed for the Spring Boot project.
    • @Configuration: This Spring configuration class analyzes the bean definition in the class of the annotation to determine the type constraint for generating a test case, that is, the type of the bean used by the generated test case. For details, see Spring Boot Project Sample.
    • @SpringBootApplication: This Spring configuration class analyzes the definitions of all beans injected in the Spring Boot project to determine the type constraint for generating a test case, that is, the type of the bean used by the generated test case.

    Test type

    Test type, which must be Unit tests.

    Active profiles

    A profile is a mechanism used to configure and activate different bean definitions and configuration attributes based on specific environments or conditions. It works with a Spring configuration class to analyze the definitions of injected beans to determine the type constraint for generating a test case, that is, the type of the bean used by the generated test case.

  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 3 Generating test cases
    Figure 4 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 5 Success
    Figure 6 Folder of generated UT cases
    Figure 7 Folder of generated UT cases (timeout classes)

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