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();
	}
}
相关推荐
weixin_408099673 分钟前
2026 AI生成图片快速去水印的5种实测方法(附在线工具 + Python/Java/PHP API代码)
java·人工智能·python·api接口·ai去水印·石榴智能·自动去水印
风筝在晴天搁浅3 分钟前
快手 CodeTop LeetCode 227.基本计算器Ⅱ
java·开发语言
JAVA面经实录9177 分钟前
RabbitMQ全套学习知识手册
java·rabbitmq
我是一颗柠檬9 分钟前
【Redis】哨兵机制Day10
数据库·redis·后端·缓存
0xDevNull9 分钟前
Java实战面试题(一)
java·开发语言
好家伙VCC9 分钟前
动态因子图谱+滚动SHAP重构量化模型可解释性
java·人工智能·重构
椰椰椰耶12 分钟前
[SpringCloud][11] Nacos 负载均衡,服务下线、权重配置、同集群优先访问
java·spring cloud·负载均衡
学编程的小程13 分钟前
配置范式演进:XML、JavaConfig 与 Spring Boot
xml·spring boot·后端
Miss roro13 分钟前
通用OA能不能替代专业法务系统?钉钉飞书和律杏法务云的实测对比
java·钉钉·飞书·法律科技·企业诉讼管理·法务管理系统
不是光头 强14 分钟前
feign-list-param-crash-cpp
java·数据结构·list