idea 自動生成 Junit單元測試插件Junit Generator


背景

在做項目過程中,經常涉及 DAO 層的 單元測試,在不斷的重復勞動過程中,看到 有設置 自動生成測試模版,就嘗試 使用,減少不必要的 勞動。程序員的工作不就是盡可能實現自動化嗎。哈哈

准備

安裝 JUnit Generator V2.0插件,網上很多,可參考文章之一

使用

  • 模版設置1

這里都通過 ${param} 的形式定義變量。

我是設置成項目 application module 的 test包下

project-name/application/src/test/java/${PACKAGE}/${FILENAME}

  • 模版的設置2:

參考一些文檔,做了一些修改, 如空格, 一些固定的 注解 如 @SpringBootTest等,

還可以自定義 變量/(自己看起來比較像 velocity的語法)。

以下模版可以直接使用(XXXService的測試模版

######################################################################################## 
## 
## 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. 
##
## 首字母大寫 
#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
## 截取字段 自定義substring
#macro (substr $strIn)$strIn.substring(0, $strIn.indexOf("Impl"))#end


#foreach ($entry in $entryList) 
#set( $testClass="${entry.className}Test") 

## 自定義賦值
#set( $service="#substr(${entry.className})") 
## 
package $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 
*/ 
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest
public class $testClass { 

    @Autowired
    $service #down($service);
    
    @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

idea JunitGenerator 的使用

我用的mac pro, 快捷鍵是 control + enter

確認后,就生成測試方法了。

總結

  • 工具的使用真的很方便
  • 路徑的設置可以做成通用的,目前是基於當前項目的


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM