提高单测效率

使用JunitGeneratorV2.0生成基于Mockito框架的单元测试类模板

1.安装插件

Setting -> Plugins -> JunitGenerator V2.0

IDEA安装JunitGeneratorV2.0插件

2.配置属性

${SOURCEPATH}/test/${PACKAGE}/${FILENAME}
=> ${SOURCEPATH}/../../test/java/${PACKAGE}/${FILENAME}

Junit3
=> Junit4

IDEA配置JunitGenerator属性

3.配置模板

有坑提前知:尝试$USER、${USER}、$DATE、${DATE}不生效

######################################################################################## 
## 
## 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 test.$entry.packageName; 

import org.junit.Test; 
import org.junit.Before; 
import org.junit.After; 

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

@Before
public void before() throws Exception { 
} 

@After
public void after() throws Exception { 
} 

#foreach($method in $entry.methodList) 
/** 
* 
* Method: $method.signature 
* 
*/ 
@Test
public void test#cap(${method.name})() throws Exception { 
//TODO: Test goes here... 
} 

#end 

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

#end 
} 
#end

=====>

########################################################################################
##
## 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
#macro (lower $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.Test;
import org.junit.Before;
import org.junit.After;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
 
/**
* ${entry.className} Tester.
*
* @author $USER
* @since <pre>$today</pre>
* @version 1.0
*/
public class $testClass {
 
    @InjectMocks
    private ${entry.className} #lower(${entry.className});
 
#foreach($field in $entry.fieldList)
    @Mock
    private #cap(${field}) ${field};
#end
 
    @Before
    public void before() throws Exception {
        MockitoAnnotations.initMocks(this);
    }
     
    @After
    public void after() throws Exception {
         
    }
     
#foreach($method in $entry.methodList)
    /**
    *
    * Method: $method.signature
    *
    */
    @Test
    public void test#cap(${method.name})() throws Exception {
        //Step 0: Prepare test data
         
        //Step 1: Record
         
        //Step 2: Replay
         
        //Step 3: Verify
         
    }
     
#end
     
#foreach($method in $entry.privateMethodList)
    /**
    *
    * Method: $method.signature
    *
    */
    @Test
    public void test#cap(${method.name})() throws Exception {
        //Step 0: Prepare test data
         
        //Step 1: Record
         
        //Step 2: Replay
         
        //Step 3: Verify
         
    #foreach($string in $method.reflectionCode)
        $string
    #end
    }
 
#end
}
#end
IDEA配置Junit模板

4.生成测试类

ALT + INSERT -> Junit Test -> Junit 4

IDEA生成测试类
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。