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();
	}
}
相关推荐
用户312687487720几秒前
volatile 到底能保证什么?JMM 与内存屏障深度剖析
java
烂蜻蜓5 分钟前
Flask入门教程(二十四):请求对象API——获取客户端数据的完整指南
后端·python·flask
geovindu7 分钟前
CSharp: Template Method Pattern
开发语言·后端·c#·.net·模板方法模式·行为模式
步行cgn13 分钟前
Spring Boot application.properties 配置文件详解
后端
苏渡苇13 分钟前
HandlerInterceptor 和 WebFilter:两套 HTTP 请求拦截
spring boot·spring·http·spring cloud
分支预测失败15 分钟前
RISC-V 虚拟内存与 MMU 实战:从 Sv39 页表到 Linux 地址空间
后端
步行cgn16 分钟前
@SpringBootTest 详解:Spring Boot 测试的核心注解
后端
吴声子夜歌19 分钟前
Guava——集合(二)
java·guava
xiaoqiMikko19 分钟前
一句“为了保持同步”的注释,正好打破了同步 —— Netty 响应错配漏洞实测
java
对象存储与RustFS21 分钟前
给 RustFS 拆多租户权限:IAM 用户、组与策略的实战
后端·rust·开源