1. 前提條件
- 開發(fā)環(huán)境已正確配置
- 工程已解決JUnit依賴關(guān)系(pom.xml)
- 我用的是4.12版本:
<dependency>
???? <groupId>junit</groupId>
???? <artifactId>junit</artifactId>
???? <version>4.12</version>
</dependency></pre>
2. IDEA中JUnit配置
(1) 打開Settings窗口搜索junit诡蜓,如圖(兩個插件都勾選添加):
(2) JUnitGenerator V2.0插件,可以幫助我們自動生成測試代碼胰挑。如果搜索junit沒有JUnitGenerator V2.0時蔓罚,如下圖操作(下載添加):
更改生成文件目錄步驟
打開File->Settings
搜索junit椿肩,找到JUnit Generator
修改Properties中Output Path為測試用例生成的目錄,這里修改為java目錄:? ?
${SOURCEPATH}/../../test/java/${PACKAGE}/${FILENAME}
?
切換到JUnit 4選項卡豺谈,可以修改生成測試用例的模板郑象,比如類名、包名
剩下幾個配置的更改:
這里把Junit4那一欄的所有配置都拉出來了茬末,如果下次要配置的話厂榛,粘貼好了。
具體修改的地方:
包名丽惭、import引入击奶、@注解
########################################################################################
##
## 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;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
/**
* ${entry.className} Tester.
*
* @author <Authors name>
* @since <pre>$today</pre>
* @version 1.0
*/
@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
? ? ?
(3) 調(diào)用模板的方法(Alt+Insert)默認(rèn)測試所有所有方法。若想要動態(tài)個性化生成责掏,可以在所要測試的類頁面上柜砾,使用該快捷操作Ctrl + Shift + T,如下圖個性化設(shè)置:
現(xiàn)在可以通過右鍵菜單在這個類上運(yùn)行'*測試類名'來進(jìn)行測試,或通過Run → Edit Configurations來進(jìn)行。
3. Junit生成之后如何使用
一個測試類單元測試的執(zhí)行順序為:
@BeforeClass –> @Before –> @Test –> @After –> @AfterClass
每一個測試方法的調(diào)用順序為:
@Before –> @Test –> @After
? ?
Junit分三步:
1. 打注解
@RunWith(SpringRunner.class)
@SpringBootTest
(這兩個注解打完之后可以自動導(dǎo)入maven的dependency)
2. new一個class
3. 調(diào)用class中的函數(shù)
? ?
在@Before里面寫@Autowired或者new一個class换衬,Junit的核心目的其實(shí)就是把一個controller或者一個service或者一個無論什么的bean放在test環(huán)境中測試一下痰驱,test這邊的第一句應(yīng)該就是new一個class,然后后面就是調(diào)用這個class的函數(shù)瞳浦。
4. JUnit常用斷言及注解
JUnit為我們提供了一些輔助函數(shù)萄唇,他們用來幫助我們確定被測試的方法是否按照預(yù)期的效果正常工作,通常术幔,把這些輔助函數(shù)稱為斷言另萤。
斷言核心方法
注解