Spring Boot 集成测试
今天做了一个demo原本把其他的test代码给copy过来记过是不可以的。由于copy了test目录中间出现了些问题,只能自己搞一下了:
引入maven依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
直接使用代码:
/**
* user:kay三石
* time: 9:32
* desc: 测试使用 必须和项目在同一的包下
**/
@RunWith(SpringRunner.class)
@SpringBootTest(classes = FileServiceApplication.class)
public class MockClass {
@Value("${local.update.url}")
private String localPath;
@Value("${awsoss.region}")
private String ossRegion;
@Value("${alioss.region}")
private String aliOssRegion;
@Autowired
private RepositoryMapper repositoryMapper;
/**
* 测试添加
* junit测试 返回值必须为void
* @return
*/
@Test
public void addRepository(){
Repository repository = new Repository();
repository.setId(IdUtil.simpleUUID());
repository.setActive(1);
repository.setEnvType(1);
repository.setType(1);
System.out.println(localPath);
repository.setPhysicalPath(localPath);
repository.setCreateDate(new Date());
repository.setUpdateDate(new Date());
repositoryMapper.insertSelective(repository);
}
}
这样就可以完美的使用了。
总结一下:
- 只需引入一个springboot test 依赖就可以了,不需要引入其他的依赖
- test的代码必须是和项目在同一个package下,不然会出现@SpringBootTest(classes = FileServiceApplication.class) 找不到spring boot的运行环境,就不可以注入dao或service
- 标记@Test的方法的返回值必须为void
- @Value标记的参数不可以为static修饰
具体的也可参考:
参考