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();
	}
}
相关推荐
倚肆几秒前
Spring Boot 日志系统全面详解
spring boot·junit·单元测试
q***14641 分钟前
Spring Boot文件上传
java·spring boot·后端
WX-bisheyuange2 小时前
基于Spring Boot的民宿预定系统的设计与实现
java·spring boot·后端·毕业设计
码界奇点2 小时前
Java设计模式精讲从基础到实战的常见模式解析
java·开发语言·设计模式·java-ee·软件工程
卷福同学3 小时前
【AI编程】用Codebuddy+lighthouse开发AI年龄模拟网站
javascript·后端
5***79003 小时前
后端API设计趋势,GraphQL与REST对比
后端·graphql
桜吹雪3 小时前
手搓一个简易Agent
前端·人工智能·后端
William_cl3 小时前
【ASP.NET进阶】Controller层核心:Action方法全解析,从基础到避坑
后端·asp.net
q***9444 小时前
springboot接入deepseek深度求索 java
java·spring boot·后端
诗9趁年华4 小时前
深入分析线程池
java·jvm·算法