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