idea自动生成Junit单元测试插件

发表于:2021-1-28 09:19

字体: | 上一篇 | 下一篇 | 我要投稿

 作者:小烽    来源:博客园

  背景
  在做项目过程中,经常涉及DAO层的单元测试,在不断的重复劳动过程中,看到有设置自动生成测试模版,就尝试使用,减少不必要的劳动。程序员的工作不就是尽可能实现自动化吗。
  准备
  安装 JUnit Generator V2.0插件,网上很多。
  使用
  ·模版设置1
  这里都通过 ${param} 的形式定义变量。
  我是设置成项目 application module 的 test包下:
  project-name/application/src/test/java/${PACKAGE}/${FILENAME}
  ·模版的设置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
  idea JunitGenerator 的使用
  我用的mac pro, 快捷键是 control + enter
  确认后,就生成测试方法了。
  总结
  ·工具的使用真的很方便
  ·路径的设置可以做成通用的,目前是基于当前项目的

  本文内容不用于商业目的,如涉及知识产权问题,请权利人联系51Testing小编(021-64471599-8017),我们将立即处理
《2023软件测试行业现状调查报告》独家发布~

关注51Testing

联系我们

快捷面板 站点地图 联系我们 广告服务 关于我们 站长统计 发展历程

法律顾问:上海兰迪律师事务所 项棋律师
版权所有 上海博为峰软件技术股份有限公司 Copyright©51testing.com 2003-2024
投诉及意见反馈:webmaster@51testing.com; 业务联系:service@51testing.com 021-64471599-8017

沪ICP备05003035号

沪公网安备 31010102002173号