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();
	}
}
相关推荐
Sailing5 小时前
🚀客户端都超时了,你的 Go 服务还在查库?Context 取消链路不打通,连接池迟早被打爆
后端·面试·go
拖孩6 小时前
# 我又用 AI 做了个小程序:24 天,记录我这辈子认识的人
前端·后端·微信小程序
Csvn6 小时前
Nginx rewrite 与 server_name:last/break 语义、多域名匹配与伪静态
后端·nginx
JasonChen@6666 小时前
2.Tomcat 安装部署与一键自动化脚本实战
java·tomcat
AAA修煤气灶刘哥6 小时前
32 GB 内存跑起 1800 亿参数模型,代价是什么?
人工智能·后端·架构
SimonKing6 小时前
零 OCR 依赖!Firecrawl 这个 Rust 库把 Word / PPT / Excel 全变成 Markdown
java·后端·程序员
Rain的Java大神之路6 小时前
别被 ChatGPT 的打字机效果骗了:一字一字蹦,才是 LLM 自回归的命门(附大厂 Java 流式落地)
java·后端·面试
AAA修煤气灶刘哥6 小时前
同一个项目,为什么换个 Agent 又得从头解释?
后端·架构·代码规范
渣渣盟7 小时前
狂命爆肝99天,共52K字的JAVA学习笔记奉上,JAVA从入门到精通一文搞定,一文在手JAVA无忧
java·笔记·学习
BingoGo7 小时前
PHP 8.6 正式发布
后端·php