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();
	}
}
相关推荐
码事漫谈4 分钟前
Kimi K3 真实体验:全网评价整理,优缺点一次性说清楚
后端
Fanta丶6 分钟前
16.Activiti8 SpringBoot3.X 部署与测试
后端
用户208046804569 分钟前
Python3 数据类型转换新手实战指南
后端
唐青枫10 分钟前
Java Kafka 实战指南:从 Topic、分区到 Spring Boot 可靠消息处理
java
苏三说技术33 分钟前
为什么Spring要“抛弃”Feign?
后端
神奇小汤圆39 分钟前
Java代理模式深度解析:静态代理 vs 动态代理 vs CGLIB
后端
爱勇宝1 小时前
AI不会淘汰所有人,但会淘汰这6种人
前端·后端·程序员
脱胎换骨-军哥1 小时前
C++零成本抽象理论深度拆解:现代C++如何在不牺牲性能的前提下提供高级语法封装
java·开发语言·c++
Conan在掘金1 小时前
鸿蒙 ArkUI 进阶:@Extend 和 @Styles,把样式也抽成「乐高块」的正确姿势
后端
orient1 小时前
为什么需要并发编程
后端