# Spring 整合 Junit
1. 导入 maven 依赖
<dependency> | |
<groupId>org.springframework</groupId> | |
<artifactId>spring-test</artifactId> | |
<version>5.2.11.RELEASE</version> | |
</dependency> |
2. 在测试类上加上注解,类运行器:@RunWith (SpringJUnit4ClassRunner.class)
@SuppressWarnings("all") | |
@RunWith(SpringJUnit4ClassRunner.class) | |
public class SpringUserTest { | |
} |
3. 配置 Spring 环境上下文配置 @ContextConfiguration (classes = SpringConfig.class)
@SuppressWarnings("all") | |
@RunWith(SpringJUnit4ClassRunner.class) | |
@ContextConfiguration(classes = SpringConfig.class) | |
public class SpringUserTest { | |
} |
完整测试类代码
@SuppressWarnings("all") | |
@RunWith(SpringJUnit4ClassRunner.class) | |
@ContextConfiguration(classes = SpringConfig.class) | |
public class SpringUserTest { | |
@Autowired | |
private UserService userService; | |
@Test | |
public void test(){ | |
List<SpringMybatis> list = userService.getList(); | |
for(SpringMybatis i:list){ | |
System.out.println(i); | |
} | |
} | |
} |
Run Result