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();
	}
}
相关推荐
AskHarries几秒前
收到第一封推广邮件:我的 App 正在被看见
前端·后端·产品
w***H6505 分钟前
SpringBoot项目如何导入外部jar包:详细指南
spring boot·后端·jar
r***R2895 分钟前
【spring】Spring事件监听器ApplicationListener的使用与源码分析
java·后端·spring
小周在成长5 分钟前
Java `this` 关键字深度解析
后端
i***13246 分钟前
Spring Boot 整合 log4j2 日志配置教程
spring boot·单元测试·log4j
自由生长202410 分钟前
C++折叠表达式完全指南:从打印函数到空包处理的深入解析
c++·后端
tkevinjd11 分钟前
JavaTreeMap源码分析
java
爱笑的眼睛1112 分钟前
Flask应用API深度开发:从单体架构到微服务设计模式
java·人工智能·python·ai
j***121513 分钟前
计算机体系结构期末复习3:GPU架构及控制流问题
java·开发语言·架构
i***t91914 分钟前
Spring Cloud gateway 路由规则
java