springboot idea junit 单元测试

本文详细介绍如何在项目中配置JUnit4进行自动化测试,包括插件安装、测试类生成路径及模板设置,以及如何通过SpringBoot整合JUnit进行单元测试。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Step1:安装junit插件

Step2:设置测试类生成路径:

Step3:设置生成测试类模板:

按照自己的需求设置,我的是这样的:

######################################################################################## 
## 
## 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 test.$entry.packageName; 

import org.junit.Test; 
import org.junit.Before; 
import org.junit.After; 
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

/** 
* ${entry.className} Tester. 
* @author <Authors name> 
*/ 
@RunWith(SpringRunner.class)
@SpringBootTest
public class $testClass { 

@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

Step4:工程引入所需jar包

testCompile group: 'junit', name: 'junit', version: '4.12'
compile group: 'org.springframework.boot', name: 'spring-boot-starter', version: '2.1.0.RELEASE'
testCompile group: 'org.springframework.boot', name: 'spring-boot-starter-test', version: '2.1.0.RELEASE'

Step5:选择需要测试的方法右键(或快捷键alt+Insert)---Generate----JUnit Test---JUnit4

生成后的测试类如下图:

如果需要注入,直接使用@AutoWired注解注入就好。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值