springboot idea junit 單元測試

Step1:安裝junit插件

Step2:設置測試類生成路徑:

Step3:設置生成測試類模板:

按照自己的需求設置,我的是這樣的:

######################################################################################## 
## 
## 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; 
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

/** 
* ${entry.className} Tester. 
* @author <Authors name> 
*/ 
@RunWith(SpringRunner.class)
@SpringBootTest
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

Step4:工程引入所需jar包

testCompile group: 'junit', name: 'junit', version: '4.12'
compile group: 'org.springframework.boot', name: 'spring-boot-starter', version: '2.1.0.RELEASE'
testCompile group: 'org.springframework.boot', name: 'spring-boot-starter-test', version: '2.1.0.RELEASE'

Step5:選擇需要測試的方法右鍵(或快捷鍵alt+Insert)---Generate----JUnit Test---JUnit4

生成後的測試類如下圖:

如果需要注入,直接使用@AutoWired註解注入就好。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章