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();
	}
}
相关推荐
铁皮哥几秒前
【后端/Agent 开发】给你的项目配置一套 .claude/ 工作流:别再裸用 Claude Code 了!
java·windows·python·spring·github·maven·生活
铁皮饭盒6 分钟前
今天你会学到这些关键词
前端·后端
乐之者v7 分钟前
AI编程 -- codex添加代码,在intellij Idea中没有显示,如何处理?
java·ide·intellij-idea
oil欧哟14 分钟前
🤔 很长时间没写文章了,分享一下最近的一些思考
前端·后端
Walter先生16 分钟前
MCP行情数据接入配置踩坑全记录:从Claude Code到Zed八大客户端适配实战
后端·websocket·架构·实时行情数据源
2401_8788204744 分钟前
Sa-Token基础篇
java·spring boot·后端·sa-token
2301_816374331 小时前
Nginx下构建PC站点
java·运维·nginx
无所事事O_o1 小时前
JAVA应用不定时卡顿问题排查过程记录
java·优化
覆东流1 小时前
第10天:python元组
开发语言·后端·python
万事大吉CC1 小时前
【5】Django 的模板语言:页面架构设计
后端·python·django