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();
	}
}
相关推荐
IT_陈寒15 分钟前
Redis误用keys命令把生产环境搞崩了,血的教训
前端·人工智能·后端
Tangyuewei28 分钟前
给老 Spring 项目装个 AI Agent
java·人工智能·spring
她的男孩43 分钟前
多租户和数据权限怎么共存?扒完拦截器注册链路,我找到 4 个隐蔽的坑
java·后端·架构
用户8356290780511 小时前
使用 Python 设置 Excel 页眉和页脚
后端·python
步行cgn1 小时前
IoC 控制反转:从概念到 Spring 的实现
后端
爱勇宝1 小时前
没有 Fn 键关触控板?我做了一个双击即用的 Windows 小工具
前端·后端·程序员
小溪学编程1 小时前
Java BufferedReader 详解:从基础用法到性能优化
java·python·性能优化
HZero.chen1 小时前
Java 匿名类简介
java·匿名类
用户3126874877201 小时前
Java 泛型擦除到底擦掉了什么?类型安全与桥接方法深度拆解
java
暖焰核心2 小时前
继承全解——继承、默认成员函数、切片、隐藏与虚继承
java·前端·javascript