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();
	}
}
相关推荐
StockTV3 分钟前
SpringBoot对接黄金白银期货数据API
java·spring boot·后端
hsjcjh13 分钟前
窗口函数-详细讲解分析
java·服务器·前端
东北甜妹32 分钟前
Redis Cluster 操作命令
java·开发语言
techdashen42 分钟前
Rust 正式成立 Types Team:类型系统终于有了专属团队
开发语言·后端·rust
小谢小哥1 小时前
43-Kafka 核心原理与实战
后端·架构
金銀銅鐵1 小时前
[git] 如何找到已经“丢失”的 commit?
git·后端
消失的旧时光-19431 小时前
Spring Boot 核心机制之 @Conditional:从原理到实战(一次讲透)
java·spring boot·后端
石榴树下的七彩鱼1 小时前
智能抠图 API 接入实战:3 行代码实现图片自动去背景(Python / Java / PHP / JS)
java·图像处理·人工智能·python·php·api·抠图
知兀1 小时前
【Result类】(使用/不使用<T> data的情况);自带静态方法、纯数据类;
java·开发语言
Seven971 小时前
【从0到1构建一个ClaudeAgent】协作-自主Agent
java