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();
	}
}
相关推荐
利威尔xu1 小时前
Go GMP 调度器 10 个高频追问:3 个线上故障让 P99 从 80ms 飙到 2s、10 万 goroutine 泄漏
开发语言·后端·面试·性能优化·golang
中电金信1 小时前
从业务调优到分层缓存:DeepSeek V4 Flash的PPU实践
java·开发语言
梦影_1 小时前
MySQL进阶教程(二)—— 储存过程与触发器(下)
开发语言·数据库·后端·sql·mysql·oracle·数据库开发
IT_陈寒1 小时前
为什么你应该学习Java?
前端·人工智能·后端
snow@li1 小时前
ruoyi-vue-pro/芋道源码:一键改包名/用项目自带的 ‌ProjectReactor‌ 程序/彻底、省事
java
AINative软件工程1 小时前
LLM 应用的 Read-Your-Writes 一致性工程实践:写完就读,模型却返回旧数据
后端
熊出没1 小时前
CommitLoom 使用教程:在 IntelliJ IDEA 中自动生成 Git 提交信息
java·git·intellij-idea
FfHUCisI1 小时前
无锁 Reactor 网络模型与 gnet / evio 架构拆解
服务器·开发语言·后端·golang
高频因子挖掘机1 小时前
从第一行 Python 代码开始,个人量化项目应该先接入历史行情还是实时行情?
后端·github·api
步行cgn1 小时前
Spring MVC POST 请求乱码:原因与完整解决方案
java·spring·mvc