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();
	}
}
相关推荐
309556666几秒前
股票数据落库前的去重与主键设计:一次 SQLite 写入实录
java·开发语言·jvm·数据库·redis·python·sqlite
AAA修煤气灶刘哥2 分钟前
同一个模型,为什么还要专门练一遍怎么用工具?
人工智能·后端·算法
Cosolar4 分钟前
LoopForge:把团队的交付经验变成可执行的工程资产
人工智能·后端·agent
tong_li4 分钟前
给娃做个 AI 陪写作业助手:LangChain4j 入门(四):向量库搬进 Qdrant(向量数据库入门)
后端
ikgy6 分钟前
JAVA EE:5.锁机制、CAS 与 JUC 体系全解析
java·java-ee
Rain的Java大神之路15 分钟前
大表 JOIN 不必靠堆内存:Dojao 的跨源 SQL、减量溢写与 Python UDF 实战
java·大数据·后端
她的男孩41 分钟前
会签选了两个人,待办却生成一条谁也办不了:Forge Admin 按人拆开自选审批人
spring boot·低代码·工作流引擎
AAA修煤气灶刘哥1 小时前
两个 Agent 各自改得都对,合在一起为什么出错?
人工智能·后端·算法
Cosolar1 小时前
概率性计算的系统工程:从 Super-agent 与 Octop 看多 Agent 架构的收敛
人工智能·后端·架构
bai2731 小时前
npm 在 cmd 里能跑,Node 的 spawn 一调就 EINVAL:Windows 上 .cmd 的六种调法实测
javascript·后端