作为一个开发者,越发觉得单元测试是必须的,至于单元测试是由开发编写还是测试编写,我觉得还得看公司的技术氛围。有一个好的编写单元测试习惯的开发者,代码质量肯定是很好的,可以随时校验自己开发和改写接口的快速检查工具。也避免了测试提的bug多而影响个人绩效(有些公司把bug计入考核范围内)。而作为开发者又不想过多花费时间在单元测试中(毕竟一般开发很忙的),所以本文提供了一种快速自定义的生成单元测试的方法。
本人主要介绍的是单元测试的生成,而运行当然是测试比较擅长的啦(这就是社会的分工)~ 主要使用的工具是Intelij Idea开发工具的插件—Junit Generator。这个插件支持Junit 3和Junit 4编写,本人只详细介绍Junit 4编写,junit3的编写很类同。
1、安装Junit Generator 2插件
可以使用离线安装或者在线安装,本人主要介绍在线安装,在线安装有时候会感觉一直报超时连接不上,那请按照下面的步骤操作吧。
最主要是勾选HTTP Proxy Settings中的Auto-detect proxy settings,这个时候在输入插件名字Junit Generator V2.0 下载安装即可。
2、Junit Generator 2 配置
首先我们配置生成文件的目录,目录当然可以随意指定喽,但是为了能够和测试所使用的框架兼容,最好将测试文件的输出目录设置为src/test/java中,因为有些测试框架默认的输出目录就是这个,那需要怎么设置呢?请看下图~
在如上图指示中的Output Path 中的路径重写填写为:${SOURCEPATH}/../../test/java/${PACKAGE}/${FILENAME} 这个路径和当前需要生成测试文件的路径包名是一样的,只不过他在src/test/java目录下的。其次,选择的模板是Junit 4。
在配置好输出路径之后,我们还需要配置Junit 4的模板输出样式~ 在本人的摸索下,我发现原来也可以自己去定义命令,我也尝试着仿写一些命令,比原先生成的单元测试更加全面完整。让原先那些不是很喜欢些单元测试的同学都爱上了些单元测试。怎么书写呢?直接看下面的配置~
########################################################################################
##
## 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
首字母小写 自定义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
Iterate through the list and generate testcase for every entry.
#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 tjf
* @date $today
* @version 1.0
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath*:/spring-context*.xml" })
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
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
注意哦,这个是我自己调整后的,原先的格式真不是我喜欢的格式,都是顶格写,没有分行,内容也比较简单。在关键的地方笔者也加了些中文注释, ##表示的是注释,#表示的是命令。大家可以根据自己的需要扩展命令,使生成的单元测试满足自己的要求。
那需要怎么触发生成呢? 可以在需要生成的单元测试的类中,按下快捷键 alt+ insert 出现如图选项。
最后展示一下笔者生成的样式吧~ 请看下图
package com.tairanchina.external.service.impl;
import com.tairanchina.external.service.TaiUserService;
import org.junit.After;
import org.junit.Before;
import org.junit.FixMethodOrder;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.MethodSorters;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
/**
* TaiUserServiceImpl Tester.
*
* @author tjf
* @version 1.0
* @date 12/13/2018
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath*:/spring-context*.xml"})
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class TaiUserServiceImplTest {
@Autowired
TaiUserService taiUserService;
@Before
public void before() throws Exception {
}
@After
public void after() throws Exception {
}
/**
* Method: getTaiUser(String uid)
*/
@Test
public void testGetTaiUser() throws Exception {
taiUserService.getUserInfoFromBank("1");
}
/**
* Method: getUserInfoFromBank(String userId)
*/
@Test
public void testGetUserInfoFromBank() throws Exception {
//TODO: Test goes here...
}
}
欢迎批评指正~
————————————————
版权声明:本文为优快云博主「南宫酥卿」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.youkuaiyun.com/win7system/article/details/85007818