Java automatic generation of single-test code artifact recommendation

1、 Background

Many companies have certain requirements for branch single test coverage. For example, the single test coverage must reach 60% or 80% before release.

Sometimes the construction period is relatively tight, so we should give priority to the development of functions, test functions, and then supplement unit tests.

However, writing unit tests is a waste of time. Is there a plug-in that can automatically generate unit tests to a large extent and can be simply modified?

Try to search for relevant plug-ins in the Idea plug-in library and try to use them, and find that TestMe is OK. After communicating with other students, Lie companion recommended Squaretest that he had been using. I found it was quite good after trying it out.

These two plug-ins are briefly introduced here.

2、 Recommended tools

2.1 Squaretest

2.1.1 Introduction

Advantages: the generated code is relatively neat, the generated code is relatively simple, and it helps to construct some parameters.

Disadvantages: When the Confirm Mock function is not used, the @ Mock attribute will not be generated if the attribute is injected through the @ Setter annotation when the single test code is generated for the Spring bean; If you want to implement it, you can only modify the template to support it temporarily (we will give it later).

usage method:

You can select the first one from the [Squaretest] menu at the top or use the corresponding shortcut key to create a unit test.

Generated code:

This example is relatively simple, just to show you how to use it. When the class is complex in actual use, you can realize the power of the plug-in.

Sample code:

Generated code

Official demonstration 1: selectively generate test code

Official presentation 2: Select the attributes that need to be mocked

Official example 3: write test in a single test to select the method to be tested and automatically generate test code

2.2.2 Customization

As for the default template, when generating single-test code for Spring beans, if the beans are declared in xml and the attributes are injected through @ Setter annotations, the @ Mock attribute will not be generated.

You can use the Confirm Mocks function to select the attribute that requires Mocks

The plug-in also supports adjusting the generated template:

You can also make simple modifications to the template. All @ Setters will automatically add @ Mock annotations:

Add Setter annotation to the dependent annotation attribute.

2.2 TestMe

2.2.1 Introduction

Function:

Automatically generate Java JUnit 4/5, TestNG unit tests

Automatically generate Mockito mocks

Automatically generate test parameters and assertion statements

Automatically generate relevant mock methods

IDEA menu: Code ->TestMe, Code ->Generate

Advantages: When Spring's beans generate single-test code, even if the @ Component annotation annotation annotation is used, the @ Mock and @ InjectMock attributes will be automatically added when the attribute is injected through the Setter annotation.

Disadvantages: the default template will add throws exception to the generated methods

2.2.2 Customization

You can also modify the template according to your needs:

There are several problems with the default template:

1. No @ RunWith (Mockito JUnitRunner. class) annotation was added on the class

2. It is not necessary that the unit test method will be followed by the throw exception by default

3. The content of TestMe Footer.java at the bottom does not need

4. There is no blank line between @ Mock and @ InjectMock

Make a copy of Junit4&mockito (the original file is read-only) and modify it

Generate a copy of Junit4&mockito template, which can be modified

3: Summary

Flexible use of unit tests to automatically generate plug-ins can save a lot of time.

You can install and try these two plug-ins, and then choose the most suitable plug-in according to your preferences.

You can also adjust the template according to your preferences.

In addition, you should not be too strict with plug-ins. The generated unit tests still need to be modified more or less by yourself, such as modifying the parameters and adding several assertions.

Related Articles

Explore More Special Offers

  1. Short Message Service(SMS) & Mail Service

    50,000 email package starts as low as USD 1.99, 120 short messages start at only USD 1.00

phone Contact Us