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();
	}
}
相关推荐
Sam_Deep_Thinking5 分钟前
聊聊开闭原则,以及它在Spring里的样子
java·后端·spring·程序员·开闭原则
IT_陈寒16 分钟前
Redis的Lua脚本居然把我的集群搞崩了!
前端·人工智能·后端
SimonKing18 分钟前
开源神器 Navop:数据库+SSH+终端+AI,一个应用全搞定
java·后端·程序员
爱码猿22 分钟前
SpringBoot+MybatisPlus动态数据源
java·spring boot·后端
小义_23 分钟前
JDK 深度解析
java·linux·开发语言·python·面试
奈斯先生Vector43 分钟前
从“能调用”到“可运营”:AI Agent 进入多模型时代后的架构升级
java·javascript·数据库·人工智能·算法·架构·aigc
Json____1 小时前
宿舍安全卫生检查系统:Node 全栈开发实战
java·前端·数据库·毕业设计·课程设计·毕设·wwwoop.com
pnoker1 小时前
36 个驱动模块:应对协议碎片化
java·物联网·modbus·工业互联网·opc ua
码上有光1 小时前
Linux:进程控制和进程替换
java·linux·服务器·进程控制·进程替换
默辨1 小时前
我用Java后端的视角读了一遍JoyAgent-JDGenie
java·开发语言