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();
	}
}
相关推荐
青石路6 分钟前
不依赖 Spring,你会如何自实现 RabbitMQ 消息的消费(二)
后端·rabbitmq
Csvn32 分钟前
Nginx 可观测性与工程化:指标、告警与发布回滚演练
后端·nginx
光泽雨37 分钟前
IActionResult 是 ASP.NET Core Web API 中非常常用的一种控制器方法返回类型
后端·asp.net
如夏倾城2 小时前
进程状态模型详解:从三态、五态到 Linux 状态码映射
java·linux·服务器
唐老板2 小时前
前端和后端,转 AI 全栈怎么转
前端·后端
掘金安东尼2 小时前
中国移动 OAS 是什么:智能体开放服务空间、四项能力与「自研」说法的核实
人工智能·后端
小南家的青蛙2 小时前
JVM 参数调优实战指南
java·jvm
JuiceFS2 小时前
缓存命中率 99.96%,小文件数量减少 99.92%:启智机器人 JuiceFS 生产实践
人工智能·后端·机器人
程序员鱼皮3 小时前
Antigravity 保姆级实战教程,从 0 到 1 万字干货!
前端·后端·ai编程
tong_li3 小时前
给娃做个 AI 陪写作业助手:LangChain4j 入门(九):AI 分得清老大和老二(多用户隔离与 token 认证)
后端·langchain