导入 spring 集成 junit 的坐标
<!--此处需要注意的是,spring5 及以上版本要求 junit 的版本必须是 4.12 及以上--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>5.0.5.RELEASE</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency>
使用@Runwith 注解替换原来的运行期
使用@ContextConfiguration 指定配置文件或配置类
@RunWith(SpringJUnit4ClassRunner.class) //加载spring核心配置文件 //@ContextConfiguration(value = {"classpath:applicationContext.xml"}) //加载spring核心配置类 @ContextConfiguration(classes = {SpringConfiguration.class}) public class SpringJunitTest { //... }
使用@AutoWired 注入需要测试的对象
创建测试方法进行测试
@Autowired private UserService userService; @Test public void testUserService(){ userService.save(); }
上一篇
2022-03-26
下一篇
2022-03-20