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();
	}
}
相关推荐
vanuan4 分钟前
给你的A2A-Agent加把锁-认证鉴权实战指南
后端
Yeats_Liao12 分钟前
14:Servlet中的页面跳转-Java Web
java·后端·架构
未秃头的程序猿17 分钟前
告别"if-else地狱"!Java 21模式匹配,代码优雅了10倍
java·后端·面试
武子康38 分钟前
调查研究-201 Rust 里的 dev build 和 release build:为什么同一份代码性能差这么多?
后端·架构·rust
鹤望兰67540 分钟前
字节跳动国际支付-后端开发-三面面经
java
石小石Orz41 分钟前
AI具身交互:实现一个会说话的3D虚拟伴侣
前端·人工智能·后端
Ai拆代码的曹操42 分钟前
容器 CPU Throttling 有多坑?K8s CFS 限制让 P99 慢了 16 倍
后端
夕阳与风馨43 分钟前
大文件(20GB+)SFTP 下载模块设计与实现
后端·架构
Flittly1 小时前
【AgentScope Java新手村系列】(14)人机交互
java·spring boot·spring