Idea单元测试Junit Generator设置

0. setting--->plugins--->brose repostories-->输入JUnitGenerator V2.0

1.junit generator自动生成测试代码缺省和java类在同一报下,不匹配maven项目标准测试目录。修改设置,查询junit Generator
2.outpath设置为${SOURCEPATH}/../../test/java/${PACKAGE}/${FILENAME}【default template选junit4】

3.点击Properties最右侧的Junit4,修改模板。将@since <pre>$date</pre> 改为@since <pre>$today</pre>。 这样就不会出现日期乱码。

package test.$entry.packageName;  改为 package  $entry.packageName;   符合maven的对应目录。
4.自动生成测试代码的快捷键:在待编写测试的java类源码块上按快捷键:alt+insert 或ctrl+shift+T

######################################################################################## 
## 
## 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 
## 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.Test; 
import org.junit.Before; 
import org.junit.After; 

/** 
* ${entry.className} Tester. 
* @author zhongmaming
* @date $today
* @version 1.0 
*/ 
public class $testClass { 

    @Before
    public void before() throws Exception { 
    } 
    
    @After
    public void after() throws Exception { 
    } 
    
    #foreach($method in $entry.methodList) 
    
    /** 
     * @see $entry.packageName.${entry.className}#$method.signature 
     */ 
    @Test
    public void test#cap(${method.name})() throws Exception { 
        //TODO: Test goes here... 
    } 
    #end 

#foreach($method in $entry.privateMethodList)
/**
*
*/
@Test
public void test#cap(${method.name})() throws Exception {
#foreach($string in $method.reflectionCode)
$string
#end
}
#end
}
#end
原文地址:https://www.cnblogs.com/exmyth/p/12447944.html