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();
	}
}
相关推荐
爱勇宝1 小时前
初创公司的“自己人”,到底能当多久?
前端·后端·程序员
掘金者阿豪1 小时前
Mac mini Intel 报错 DNS_PROBE_FINISHED_BAD_CONFIG 完整排障指南
后端
程序猿乐锅1 小时前
【计算机组成原理 | 第六章】中央处理器 CPU
java·开发语言
大模型丫丫1 小时前
如何提升单体 Spring Boot 应用的并发数?
java·spring boot·后端
Terra.K1 小时前
ThreadLocal解决一个线程里面跨层传递问题
java·开发语言·后端
RuoyiOffice2 小时前
SpringBoot3+Vue3 最推荐的开源 OA 系统 2026:流程驱动、资源闭环、多端互通
spring boot·vue3·flowable·oa·协同办公·spring boot 3·开源oa
泡海椒2 小时前
低代码规则开发:JQuick-Java声明式配置驱动开发落地教程
java·低代码·rxjava
Lost of 程序猿2 小时前
ASP.NET Core API 版本管理与契约演进:船岸版本碎片化下的兼容性工程
后端·asp.net
吃饱了得干活2 小时前
Redis 从单机到集群:持久化、主从复制、哨兵与集群完全指南
redis·后端
weixin_440730502 小时前
socket解决多次输入问题-大数据接收问题-模块动态导入
java·开发语言·servlet