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();
	}
}
相关推荐
用户8356290780517 小时前
如何使用 Python 将 PowerPoint 转换为 PDF 文档
后端·python
倔强的石头_7 小时前
从 8 行报错到一条故障链:我用蓝耘 MaaS 做了一个 AI 日志分析助手
后端
敲个大西瓜7 小时前
Spring Could Alibaba 核心面试题
java·后端·spring
用户8356290780517 小时前
如何使用 Python 为 PowerPoint 幻灯片添加切换效果
后端·python
四千岁7 小时前
RAG系统中的分块
前端·javascript·后端
Young_Gnay7 小时前
.NET 之 WebApi学习笔记 (持续更新)
后端·c#
骇客野人7 小时前
Java SSO 统一认证方案
java·开发语言
程序员cxuan7 小时前
Claude Code :如何最大化你的 Session 价值
人工智能·后端·程序员
START_GAME7 小时前
MSSQL$SQL2016
java·服务器·前端