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();
	}
}
相关推荐
林石工作室10 分钟前
体育直播APP从0到1搭建指南:技术选型、核心模块与部署实战
spring boot·流媒体·app搭建·体育直播app·星逐赛事
IT_陈寒17 分钟前
JavaScript的这个隐式转换特性差点让我加班到凌晨
前端·人工智能·后端
Wang's Blog18 分钟前
Java框架快速入门: Spring Security+OAuth2之自定义认证过滤器实现JSON登录
java·spring·json
用户83562907805140 分钟前
使用 Python 在 Excel 中添加和编辑形状
后端·python
用户2049375549543 分钟前
端侧语音部署踩坑:模型能跑不等于终端真的能用
后端·算法
n8n1 小时前
Spring AI 结构化输出实战:让模型返回 JSON,而非自由文本
后端
Wang's Blog1 小时前
Java框架快速入门: Spring Security+OAuth2之密码验证规则与自定义注解
java·数据库·spring
一嘴一个橘子1 小时前
java - redis 缓存击穿 - 互斥锁
java
shehuiyuelaiyuehao1 小时前
算法39,位运算,消失的两个数字
java·数据结构·算法