1. 前提条件
- 开发环境已正确配置
- 工程已解决JUnit依赖关系(pom.xml)
- 我用的是4.12版本:
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency></pre>
2. IDEA中JUnit配置
(1) 打开Settings窗口搜索junit,如图(两个插件都勾选添加):
![](https://img.haomeiwen.com/i10743979/78b6719da6ffa8f4.jpg)
(2) JUnitGenerator V2.0插件,可以帮助我们自动生成测试代码。如果搜索junit没有JUnitGenerator V2.0时,如下图操作(下载添加):
![](https://img.haomeiwen.com/i10743979/6ea16dd7c0417e6f.jpg)
更改生成文件目录步骤
打开File->Settings
搜索junit,找到JUnit Generator
修改Properties中Output Path为测试用例生成的目录,这里修改为java目录:
${SOURCEPATH}/../../test/java/${PACKAGE}/${FILENAME}
切换到JUnit 4选项卡,可以修改生成测试用例的模板,比如类名、包名
剩下几个配置的更改:
![](https://img.haomeiwen.com/i10743979/a5499e23a62235fb.png)
这里把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
![](https://img.haomeiwen.com/i10743979/fa7f02457caac669.jpg)
![](https://img.haomeiwen.com/i10743979/b51f0de39b7b33d5.jpg)
(3) 调用模板的方法(Alt+Insert)默认测试所有所有方法。若想要动态个性化生成,可以在所要测试的类页面上,使用该快捷操作Ctrl + Shift + T,如下图个性化设置:
![](https://img.haomeiwen.com/i10743979/fda167ca5abb9e20.jpg)
![](https://img.haomeiwen.com/i10743979/c1a6528dab067d42.jpg)
现在可以通过右键菜单在这个类上运行'*测试类名'来进行测试,或通过Run → Edit Configurations来进行。
![](https://img.haomeiwen.com/i10743979/3496f6ca9efe9d2e.jpg)
3. Junit生成之后如何使用
一个测试类单元测试的执行顺序为:
@BeforeClass –> @Before –> @Test –> @After –> @AfterClass
每一个测试方法的调用顺序为:
@Before –> @Test –> @After
Junit分三步:
1. 打注解
@RunWith(SpringRunner.class)
@SpringBootTest
(这两个注解打完之后可以自动导入maven的dependency)
2. new一个class
3. 调用class中的函数
在@Before里面写@Autowired或者new一个class,Junit的核心目的其实就是把一个controller或者一个service或者一个无论什么的bean放在test环境中测试一下,test这边的第一句应该就是new一个class,然后后面就是调用这个class的函数。
4. JUnit常用断言及注解
JUnit为我们提供了一些辅助函数,他们用来帮助我们确定被测试的方法是否按照预期的效果正常工作,通常,把这些辅助函数称为断言。
断言核心方法
![](https://img.haomeiwen.com/i10743979/b0267cc7462b9d6f.jpg)
注解
![](https://img.haomeiwen.com/i10743979/50838121d6936f3d.jpg)
网友评论