1.1 IDEA JUnitGenerator + Junit 5 + Mockito

介绍

        JUnitGenerator 2.0 插件:能自动生成类的测试方法(空方法)

安装

        IEDA -> File  -> settings  -> Plugins  -> 搜索JunitG 安装JUnitGenerator 2.0  -> 重启IDEA

                  

 设置

1. 设置文件生成路径

        IEDA -> File  -> settings  ... 

                

        其中  ${SOURCEPATH}/../../test/java/${PACKAGE}/${FILENAME},红色部分为test存放路径

2. 修改junit5 模板

        将下列代码替换到

                 

######################################################################################## 
## 
## Available variables: 
##         $entryList.methodList - List of method composites 
##         $entryList.privateMethodList - List of private method composites 
##         $entryList.fieldList - ArrayList of class scope field names 
##         $entryList.className - class name 
##         $entryList.packageName - package name 
##         $today - Todays date in MM/dd/yyyy format 
## 
##            MethodComposite variables: 
##                $method.name - Method Name 
##                $method.signature - Full method signature in String form 
##                $method.reflectionCode - list of strings representing commented out reflection code to access method (Private Methods) 
##                $method.paramNames - List of Strings representing the method's parameters' names 
##                $method.paramClasses - List of Strings representing the method's parameters' classes 
## 
## You can configure the output class name using "testClass" variable below. 
## Here are some examples: 
## Test${entry.ClassName} - will produce TestSomeClass 
## ${entry.className}Test - will produce SomeClassTest 
## 
######################################################################################## 
## 
## 首字母大写 
#macro (cap $strIn)$strIn.valueOf($strIn.charAt(0)).toUpperCase()$strIn.substring(1)#end 
## 首字母小写 自定义down
#macro (down $strIn)$strIn.valueOf($strIn.charAt(0)).toLowerCase()$strIn.substring(1)#end
## Iterate through the list and generate testcase for every entry. 
#foreach ($entry in $entryList) 
#set( $testClass="${entry.className}Test") 
## 
package $entry.packageName; 

import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;


/** 
 * ${entry.className} Tester. 
 * 
 * @author <Authors name> 
 * @version 1.0 
 * @since <pre>$today</pre> 
 */ 
@SpringBootTest
public class $testClass { 

    @Autowired
    private ${entry.className} #down(${entry.className});

    @BeforeEach
    @DisplayName("Each unit test method is executed once before execution")
    public void before() throws Exception {
    }

    @AfterEach
    @DisplayName("Each unit test method is executed once before execution")
    public void after() throws Exception {
    }

#foreach($method in $entry.methodList) 

    @Test
    @DisplayName("Method description: ...")
    public void test#cap(${method.name})() throws Exception { 
        //TODO: Test goes here... 
    } 

#end 

#foreach($method in $entry.privateMethodList) 

    @Test
    @DisplayName("Method description: ...")
    public void test#cap(${method.name})() throws Exception { 
        //TODO: Test goes here... 
    #foreach($string in $method.reflectionCode) 
    $string 
    #end 
    } 

#end 
} 
#end

使用

类名右键  -> Generate   -> Junit Test    -> Junit4

        

效果

                 

posted @ 2021-08-24 14:37  随风落木  阅读(23)  评论(0编辑  收藏  举报  来源