One click automatic generation of Java JUnit test code artifact Gen test plugin introduction

Time:2020-2-26

gen-test-plugin

In our daily code writing process, we often need to write test cases for the code.

With the increasing demand for code quality, many companies begin to use code test coverage as an indicator of QA.

This framework can generate JUnit test cases corresponding to all codes with one click, saving precious time for your life.

Characteristic

  • Support for junit4 / junit5 generation
  • Support JDK7
  • Support custom build template

Update record

Update record

gen-test

Used to generate junit4 / junit5 unit tests.

Introduce

<plugin>
    <groupId>com.github.houbb</groupId>
    <artifactId>gen-test-plugin</artifactId>
    <version>0.0.1</version>
</plugin>

Attribute specification

attribute Explain Default value type Remarks
isOverwriteWhenExists If the test file already exists, do you want to overwrite it false Character string Do not overwrite by default
encoding Item encoding utf-8 Character string
includes Include file regular **\/*.java Character string Default to all java files
excludes Exclude file regularity Character string Do not exclude by default
junitVersion JUnit version 4 Character string Junit4 by default

Function

Command line direct execution

mvn com.github.houbb:gen-test-plugin:0.0.1:gen-test

Run the [Gen test] plug-in directlygen-testYou can generate the corresponding test class in the project test directory.

Effect

For example, the project has the following categories:

public class HelloTestDemo {

    public static String hello() {
        return "hello";
    }

}

Then generate the corresponding test file:

package com.github.houbb.gen.maven.plugin.util;

import org.junit.Test;

/**
* HelloTestDemo Tester.
*
* @author houbinbin
* @since Tue Nov 14 16:01:53 CST 2017
* @version 1.0
*/
public class HelloTestDemoTest {

    /**
    *
    * Method: hello()
    */
    @Test
    public void helloTest() throws Exception {
    }

}

Junit5 template

If you want to customize it, you can do it in Maven projectresourcesCreate file under folderjunitTest5.ftl, based on the following.

Junit4 template

The above file generation is based on the following template (FreeMarker syntax)

If you want to customize it, you can do it in Maven projectresourcesCreate file under folderjunitTest4.ftl, based on the following.

  • entry

Entity class information to be generated.

${entry. Packagename} package name

${entry. Classname} class name

Entry.methodlist gets a list of all non private / non main() methods. The private method list is not provided at this time. It can be added later.

  • today

Current date. Consider adding formatting configuration later.

  • method

Properties of method

Signature of ${method. Signature} method

${method. Name} method name

${method. Comment} method comment

Code in the body of ${method. Sourcecode} method

package ${entry.packageName};

import org.junit.Test;

/**
* ${entry.className} Tester.
*
* @author houbinbin
* @since ${today}
* @version 1.0
*/
public class ${entry.className}Test {

<#if entry.methodList??>
    <#list entry.methodList as method>
    /**
    *
    * Method: ${method.signature}
    */
    @Test
    public void ${method.name}Test() throws Exception {
    }

    </#list>
<#else>

</#if>

}

Expanding reading

IDoc – automatically generate code documents

Data factory – automatically generate test objects according to class

Source learning

Gen test plugin is open source. You can click the link to learn the relevant source code.

If you have any help, you can give a star to support the author.

official account

More technical articles and interesting life are in the old horse’s west wind.
One click automatic generation of Java JUnit test code artifact Gen test plugin introduction