每次对类的方法进行测试,都是手动创建一个Test测试类,然后编写同名的方法,每次这样都会很繁琐,IDEA开发工具提供了自动生成测试类的插件JUnitGenerator
,接下来介绍该插件的安装与使用。
安装插件JUnitGenerator
打开IDEA,依次点击:Preferences-->plugins-->Marketplace, 搜索JUnitGenerator
安装好后把该插件勾上使用(默认安装就已勾选)
插件配置
安装成功后可以找到关于JUnit自动生成测试类的相关配置,如下图所示:
配置参数介绍:
- Medhod Generation Style: 方法的生成的风格(ParamName表示方法的参数名就是原测试方法的参数名)
- Output Path: 配置测试类生成的地址
- Default Template: Junit测试类的模版,根据以来的Jar包选择 我这里使用的是
<dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> 复制代码
下图是Junit测试类不同版本的代码模版对比:
##Ava1LabLeVar1abLeS: sentryList . methodList - List of method composites SentryList . privateMethodiist - List or private method composites SentryList . fieldList - ArrayList of class scope field names SentryList . className - cCLass name SentryList . packageName - package name $ today - Todays date in NN / dd / yyyy tormat Methodcomposite variabLes : Smethod.name- Method Nane Smethod . signature - FulL method signature in String form Smethod . rerlectionCode - List of strings representing commented out ref Smethod . paramNaaes - List of strings representing the method ' s paramete Smethod . paramClasses - List of Strings representing the methods parane a You can configure the output class name using “ testClass ” variable beLow . Here are some exampLes : # Test $( entry . ClassMame }- wiLl produce TestsomeClass ##$ entry . cLassName ) Test - wiLL produce SomeCLaSsTeSt 件 # macro ( cap $ strIn )$ strIn .value0f($ strIn . charAt (8)). toUpperCase ()$ strIn . substring (1)# end # Iterate through the list and generate testcase for eVery entry . # roreach ($ entry in $ entryList ) # set ($ testCLass =“$ fentry . classNameTest ”) package test .$ entry . packageName ; import org . junit . Test ; import org . junit . Before ; import org . junit : After ;
# 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 H H Methodcomposite variables : $method.name- Method Name $ method . signature - Full method signature in String form $ method . reflectionCode - List of strings representing commented out ref $ method . paramNames - List of Strings representing the method ' s parameter $ method . paramCLasses - List of Strings representing the method ' s paramet t ## You can configure the output class name using " testClass " variable below . # Here are some examples : ## Test $ fentry . ClassName }- will produce TestSomeCtass ##$ fentry . cassName ) Test - wil produce SomeCaSSTeSt # macro ( cap $ strIn )$ strIn .value0f($ 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 junit . framework . Test ; import junit . framework . Testsuite ; import junit . framework . TestCase ;
如何使用
打开你所想要生成测试类的代码,在任意位置右击,如图所示:
选择项目使用的test包版本、类名、需要测试的方法等设置
这就是生成最终测试类的效果了
import org . junit . Test ; aimport static org . junit . Assert .*; public cLlass DateUtilTest @ Test public void strToLocalDateTime (){ @ Test public void timeStamp2Date(){ @ Test public void testTimeStamp2Date(){ @ Test public void timeStamp2UTC(){
至于方法体内部的内容就需要自己去补充,通过这种方式可以快速的去给对应的目标类生成测试方法,也可以很清晰的知道哪些方法没有对应的测试代码,便于后续的补充和测试覆盖率的提升。