1:官方下载 junitgenerator-v2 插件,安装,重启

2:settings-->other settings-->junit generator

ehyJQf+pOAAAAAElFTkSuQmCC

修改output path为${SOURCEPATH}/../../test/java/${PACKAGE}/${FILENAME}

3:修改junit 4模板

#SpringBootTest  启动类目前还没有找到参数,目前可以根据use 选择project settings

AoHHZV5OpV5U2MF8AE+Kn6FGgD4FQ76pqoI7E5nJgHwAT4qfoUaAPgVDjoq83Qq86bGCuADfFT8CjUA8Csc9E1VEdidzkwC4AN8VPwKNfAraw8IHlLzZfwAAAAASUVORK5CYII=

如下:junit4 模板稍作修改

1:$date 改为 $today 解决日期乱码

2:加入SpringBootTest  springboot test配置,@slf4j,@autowired 注入test类及类名称修改

########################################################################################

##

## 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 lombok.extern.slf4j.Slf4j;

import org.junit.Test;

import org.junit.Before;

import org.junit.After;

import org.springframework.boot.test.context.SpringBootTest;

import org.springframework.test.context.junit4.SpringRunner;

import org.springframework.beans.factory.annotation.Autowired;

/**

* ${entry.className} Tester.

*

* @author

* @since

$today

* @version 1.0

*/

@Slf4j

@RunWith(SpringRunner.class)

@SpringBootTest(classes = ShareBenefitAdminApplication.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)

public class $testClass {

@Autowired

private ${entry.className} $entry.className.substring(0,1).toLowerCase()$entry.className.substring(1);

@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:使用,在需要创建test类的类中alt+insert -->junit test -->junit 4即可自动生成test类,默认包含该类中所有方法

注意:本文归作者所有,未经作者允许,不得转载

Logo

一站式 AI 云服务平台

更多推荐