UTgen
Feature Overview
UTgen is an automatic Java unit test generation tool designed for Maven and Gradle projects. As a plugin of IntelliJ IDEA, UTgen provides end-to-end test case generation support with out-of-the-box availability. It has the following key features:
- Users can right-click a method name, class name, class file, or package directory to generate a test case.
- A test case contains the input, method body, assertion, and comments.
- The tool ensures the maximum branch coverage and the minimum number of tests when generating test cases.
- Users can set a desired test case root directory and choose the JUnit 4 or JUnit 5 test framework.
- The tool supports two case rendering policies (retain and overwrite).
- The tool supports the mock technique and allows a test generation timeout interval.
- The tool supports JDK 8, 11, and 17.
- The tool supports Java projects developed using Spring Boot 2.x.
Procedure
UTgen can generate UT cases in the form of Java packages, files, and functions.
- 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 Spring Boot projects
- 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.
- JUnit 4
- JUnit 5
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.
The three additional parameters are used for Spring Boot projects. See Table 2.
- 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.
- 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