Spring Boot整合Junit,@RunWith和@SpringBootTest的使用

Spring Boot整合Junit,@RunWith和@SpringBootTest的使用

1、在pom.xml添加junit启动器

java 复制代码
<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-test</artifactId>
</dependency>

2、编写测试类

java 复制代码
/**
 *  main方法:
 *		ApplicationContext ac=new 
 *       			ClassPathXmlApplicationContext("classpath:applicationContext.xml");
 *  junit与spring整合:
 *      @RunWith(SpringJUnit4ClassRunner.class):让junit与spring环境进行整合
 *   	@Contextconfiguartion("classpath:applicationContext.xml")  
 */
@RunWith(SpringJUnit4ClassRunner.class) 
@SpringBootTest(classes={App.class})//App是项目的启动类名称
public class UserServiceTest {

	@Autowired
	private UserServiceImpl userServiceImpl;
	
	@Test
	public void testAddUser(){
		this.userServiceImpl.addUser();
	}
}
相关推荐
H H.6 分钟前
Java笔记:String字符串
java·开发语言·经验分享·笔记·其他
Rain的Java大神之路16 分钟前
Spring AI 还是 LangChain4j?2026 年 Java 接入大模型,我把这笔「架构赌注」的账算清楚了
java·后端·ai编程
用户1129361121933 分钟前
注释掉的接口为什么还能访问?没有 Git 也能证明改动有没有进产物
java
Csvn40 分钟前
Nginx 高可用网关:VIP 切换、proxy_protocol 与 SNI 分流实战
后端·nginx
Gopher_HBo42 分钟前
跟着一次导出,走完 runtime/pprof:从一张工单到一份文件
后端
sharemylove1 小时前
Java 异常处理 学习笔记
java·开发语言·经验分享·笔记·学习
用户69371750013841 小时前
大模型蒸馏到底在蒸什么?一篇文章搞懂知识蒸馏概念
前端·后端·github
CRZZX1 小时前
三引擎 Agent 方案设计与性能测试总结
后端·架构
bai2731 小时前
同一条 q=a+b:URLSearchParams 读成空格、decodeURIComponent 读成加号,11 条查询串打进裸 socket 的实测
javascript·后端
__underline__1 小时前
冷门Python知识点008篇:__int__、__index__与__float__,Python数值转换的真实回退顺序?
后端·python