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();
	}
}
相关推荐
啊哈一半醒3 小时前
Go Web 开发知识点详解:从 HTTP 原理到 Gin 框架
后端·go·gin
想不到一个好的ID8 小时前
Redis Cluster
后端
EatFan8 小时前
Spring Boot 4.0 升级清单:JDK 基线、Jakarta 命名空间、Observation API 与第三方组件兼容一次讲清
java·spring boot·后端
大大大大晴天9 小时前
从System.out到可观测性:Java 日志框架的演进与选型
后端
辰海Coding9 小时前
平台如何接入一台物联网设备?又该如何设计它的通信协议?
java·物联网
代码民工正电荷9 小时前
JAVA重要知识点和集合
java·开发语言
李少兄10 小时前
IDEA Apifox Helper 插件踩坑:2.0.15-243 版本“导出指定接口”参数描述丢失问题解析与修复方案
java·ide·intellij-idea
JavaGuide11 小时前
Gemini 杀疯了,反重力给我用爽了!分享 8 个 Antigravity 使用技巧
人工智能·后端
努力攀登的小k11 小时前
《新手练表:从建表到删表全操作|从零学会表操作》
java·数据库·mysql
想不到一个好的ID11 小时前
Redis 哨兵(Sentinel)
后端