Spring Boot 项目使用@SpringBootTest,@RunWith注解进行单元测试,如果使用了配置文件还要加@TestPropertySource注解。
具体代码如下:
@SpringBootTest(classes = Application.class) //启动类
@RunWith(SpringJUnit4ClassRunner.class)
@TestPropertySource("classpath:application-dev.properties") //配置文件
public class MyTest {
@Resource
private UserService userService;
@Test
public void test(){
List<UserEntity> users = userService.findAll();
users.forEach(vo->System.out.println(vo.getName()));
}
}