在我们编写程序的时候,为了实现业务功能需要写很多的方法。为了确保这些方法的正确性,保证这些方法或接口没有bug,需要针对这些接口进行测试,这样的测试我们称之为单元测试。但是编写单元测试的工作非常繁琐,有没有简便的方式来减少这种重复性工作呢?答案是肯定的,我们可以使用 JUnitGenerator 插件来帮我们自动生成单元测试测试模版。
- 其他插件:IDEA一键调用一个对象的所有的set方法插件 GenerateAllSetter 的安装配置与使用
- 其他插件:IDEA折叠pom文件中的依赖项插件 Laconic POM 的安装配置与使用
1、安装方式
1.1 在线安装方式
第一种方式,是在IDEA上搜索插件进行安装,会适配当前IDEA的版本。打开File -> Settings 界面(或者使用快捷键Ctrl+Alt+S进入到Settings设置页面),在左侧列表中找到 Plugins 菜单,在右侧的 Marketplace 页签下,搜索 “JUnitGenerator”,可以找到该插件,如下图所示,点击 Install 进行安装即可。
1.2 离线安装方式
第二种安装方式是使用离线插件进行安装。插件下载页面:https://plugins.jetbrains.com/idea ,在搜索框中输入插件名称 “JUnitGenerator” 搜索,点击出现的下拉提示,即可进入插件下载页面,选择对应安装版本下载即可。
下载完成后,进入插件市场,选择本地安装
同样是在 Settings 界面,在左侧列表中找到 Plugins 菜单,在右侧的 Installed 页签右方有个齿轮图标,点击展开菜单后选择 “Install Plugin from Disk…”,此时会打开本地文件选择框,选择你下载的插件包安装即可。
安装完成后会提示重启,重启idea后该插件就可以正常使用了。
2、插件的禁用或卸载
打开File -> Settings 界面(或者使用快捷键Ctrl+Alt+S进入),在左侧列表中找到 Plugins 菜单,在右侧的 Installed 页签下,选中需要卸载的插件如 Database Navigator, 在插件列表每个插件的右侧有个复选框,勾选表示启用插件,去掉勾选为禁用插件。在列表右侧的详情页右上角有个齿轮图标,点击后出现下拉菜单,其中Disable表示禁用(如果当前插件是禁用状态则这里的文字为Enable表示启用),Uninstall表示卸载插件。
3、插件的设置
打开 File -> Settings -> Other Settings,找到Junit Generator。
3.1 通用设置
这里都通过 {PACKAGE}/${FILENAME}。
- 路径的设置可以做成通用的,目前是基于当前项目的
3.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
4、插件的使用
比如有这样一个类文件:
public class Test {
public void print() {
System.out.println("Hello World!");
}
public void print(String msg) {
System.out.println(msg);
}
}
按快捷键Alt+Insert(也可以点击鼠标右键,选择Generate…),出现如下面板,选择"Junit",选择生成Junit3或Junit4
会生成一个测试类,如下:
public class TestTest {
@Before
public void before() throws Exception {
}
@After
public void after() throws Exception {
}
/**
*
* Method: print()
*
*/
@Test
public void testPrint() throws Exception {
//TODO: Test goes here...
}
/**
*
* Method: print(String msg)
*
*/
@Test
public void testPrintMsg() throws Exception {
//TODO: Test goes here...
}
}
评论区