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();
	}
}
相关推荐
独泪了无痕6 小时前
探索Pinyin4j-Java中的拼音转换利器
java·后端
码事漫谈6 小时前
从 Coze 居然要下线了说起
后端
道尔柯南7 小时前
C++ string 类详解:从常用接口到深拷贝,彻底掌握字符串处理
android·java·c++
想不到一个好的ID7 小时前
智能体开发
后端
皮皮林5517 小时前
为什么数据库连接池不采用 IO 多路复用?
后端
kingx38 小时前
Bugku 3122 谷歌小恐龙题解:纯离线逆向 Vault 的 AES-GCM 加密资源(附 Python 解密器)
java·逆向·ctf·bugku·aes-gcm
DevOpenClub10 小时前
职教高考控制线 API:招生类别枚举、筛选与空分数含义
java·数据库·高考
IT_陈寒11 小时前
Redis雪崩把我坑惨了,这是5年经验的教训
前端·人工智能·后端
量化分析码农11 小时前
【Python量化多因子实战 06】从因子到持仓:多因子选股实盘工程化落地
后端
贾公子11 小时前
检测链路的容量规划——用 Little's Law 算清 QPS、并发与积压
后端·python