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 分钟前
【一份基础软件的下载地址和安装地址】
java
没什么本事10 分钟前
关于C# panel 添加lable问题 -- 明确X和Y 位置错误
android·java·c#
无风听海17 分钟前
MapStaticAssets()深度解析:ASP.NET Core 静态资源交付的现代范式
后端·asp.net
dhashdoia26 分钟前
GPT-5.5 代码开发实战:Codex与Browser Use深度集成与星链4SAPI优化方案
java·数据库·人工智能·gpt·架构
xuhaoyu_cpp_java1 小时前
SpringMVC学习(二)
java·经验分享·笔记·学习·spring
geovindu1 小时前
go: Lock/Mutex Pattern
开发语言·后端·设计模式·golang·互斥锁模式
counterxing1 小时前
AI Agent 做长任务,问题到底 出在哪?
前端·后端·ai编程
aiopencode2 小时前
iOS开发中Xcode安装不完整问题解决方案与配置指南
后端·ios
该用户已不存在2 小时前
别让 Claude Code 果奔,用 Claude Code MCP 与 Skills 打造自动化开发(Part 2)
后端·ai编程·claude
TAN-90°-2 小时前
Java 3——getter和setter super()关键字
java·开发语言