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();
	}
}
相关推荐
咖啡八杯7 小时前
GoF设计模式——策略模式
java·后端·spring·设计模式
lizhongxuan8 小时前
AI Agent 上下文压缩利器 Headroom
后端
Csvn11 小时前
SSH 远程管理与安全加固 — 运维的守门之道
后端
IT_陈寒11 小时前
Python搞不定字符串编码?这破玩意坑我两小时!
前端·人工智能·后端
菜鸟谢12 小时前
Rust 智能指针完整详解
后端
java小白小12 小时前
SpringBoot(01): 初识SpringBoot,从Spring的痛点说起
spring boot
菜鸟谢12 小时前
Rust 函数完整知识点详解
后端
爱勇宝13 小时前
淡泊名利之前,先承认我们都很焦虑
前端·后端·程序员
菜鸟谢13 小时前
Rust 闭包(Closure)完整详解
后端
ServBay13 小时前
如何利用本地技术栈构建 0 成本 AI SaaS 雏形
后端·aigc·ai编程