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();
	}
}
相关推荐
逸Y 仙X1 天前
Elasticsearch时间类型实战
java·大数据·elasticsearch·搜索引擎·全文检索
Gerardisite1 天前
企微批量群发消息指南:用 QiWe 省掉人工操作
java·python·机器人·企业微信
小村儿1 天前
给 AI Agent 装上"长期记忆":Karpathy 的 LLM Wiki 思想,我做成了工具
前端·后端·ai编程
何陋轩1 天前
Spring AI实战指南:在Java项目中集成大语言模型
人工智能·后端·机器学习
青山师1 天前
动态代理深度解析:JDK与CGLIB底层实现与实战
java·设计模式·面试·动态代理·java面试·cglib
用户8356290780511 天前
Python 操作 PowerPoint 表格的创建与格式化
后端·python
forestqq1 天前
基于openeuler2403sp3的容器,打包django运行环境镜像
后端·python·django
放学后的泡泡1 天前
提供一个工作流的表设计
java·设计规范
站着1 天前
TRAE SOLO 移动端正式上线:手机也是随身工位,随时随地进入「Vibe Working」
后端
盖世英雄酱581361 天前
6000条数据执行时间9s??
数据库·后端