第一章 Spring集成Junit4
1.1 集成步骤
- 导入jar包
- spring-test-5.3.1.jar
- 指定Spring的配置文件的路径
- 【@ContextConfiguration】
- 指定Spring环境下运行Junit4的运行器
- @RunWith
1.2 示例代码
/**
* @author Chunsheng Zhang 尚硅谷
* @create 2022/3/28 14:12
*/
@ContextConfiguration(locations = "classpath:applicationContext.xml")
@RunWith(SpringJUnit4ClassRunner.class)
public class TestSpringJunit4 {
@Autowired
private DeptService deptService;
@Test
public void testService(){
//创建容器对象
// ApplicationContext context =
// new ClassPathXmlApplicationContext("applicationContext.xml");
// DeptService deptService = context.getBean("deptService", DeptServiceImpl.class);
deptService.saveDept(new Dept());
}
}
本文详细介绍了如何在Spring框架中集成Junit4进行单元测试,包括步骤如导入Spring-testjar包、配置文件路径和使用@RunWith注解。以一个简单的示例展示了如何使用@Autowired和@Test注解进行服务测试。
1976

被折叠的 条评论
为什么被折叠?



