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();
	}
}
相关推荐
用户8356290780513 小时前
无需 Office:Python 批量转换 PPT 为图片
后端·python
啊哈灵机一动3 小时前
使用golang搭建一个nes 模拟器
后端
日月云棠4 小时前
各版本JDK对比:JDK 25 特性详解
java
间彧4 小时前
SpringBoot + ShardingSphere 读写分离实战指南
后端
砍材农夫4 小时前
订单超时
后端
树獭叔叔5 小时前
06-大模型如何"学习":从梯度下降到AdamW优化器
后端·aigc·openai
用户8307196840825 小时前
Spring Boot 项目中日期处理的最佳实践
java·spring boot
得鹿5 小时前
MySQL基础架构与存储引擎、索引、事务、锁、日志
后端
程序员飞哥5 小时前
Block科技公司裁员四千人,竟然是因为 AI ?
人工智能·后端·程序员
JavaGuide5 小时前
Claude Opus 4.6 真的用不起了!我换成了国产 M2.5,实测真香!!
java·spring·ai·claude code