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();
	}
}
相关推荐
FYKJ_20109 分钟前
springboot高校实验室综合管理系统86531-计算机毕业设计、课程设计
java·vue.js·spring boot·后端·python·spring·课程设计
钱栈up1 小时前
AND t.unit_code = 'SM1':一行写死的 SQL 让配料计划页永远拿不到化验值
java·sql·微服务
parade岁月1 小时前
没有称手工具了怎么办?搓一个!AI 时代顺手造了个单文件 Mermaid ER 查看器
数据库·后端
金銀銅鐵6 小时前
类实例里还有什么——putfield、getfield 与对象传递
java·jvm·后端
织码weavecodes6 小时前
Spring Boot 接入 AI 图像生成:模型适配、图片转存与 Redis 清理
人工智能·spring boot·redis
默 语7 小时前
HarmonyOS7 事件分发控制(二):onTouchIntercept 镂空玻璃卡——圆内自己接,圆外放行
android·java·开发语言
皮皮林5517 小时前
你还在使用 WebSocket 实现实时消息推送吗?
后端
码事漫谈8 小时前
token账单到底要怎么看
后端
Cosolar8 小时前
当 Harness 标准化后,工程师还剩什么?
人工智能·后端·agent
sharemylove9 小时前
Java 异常与常用类 学习笔记
java·开发语言·经验分享·笔记·学习