目录

  1. 简介
  2. 版本及依赖引入

    1. springboot版本
    2. 我的项目局部依赖
  3. 间接应用SpringBootTest形式

    1. 代码示例
    2. 场景及优劣
  4. 最小化依赖计划

    1. 代码
    2. 思路及步骤
    3. 最小化依赖计划的长处
  5. 论断

简介

想要代码跑的稳, 集成测试还是必不可少的, 不然呈现开发环境失常, 集成环境各种问题就坑爹了。

以后我的项目对外提供各种rest接口, 通过RestTemplate做接口测试, 同时须要注入一些SpringBean, 如何应用SpringBootTest又不须要启动整个容器?

版本及依赖引入

springboot版本

<parent>    <groupId>org.springframework.boot</groupId>    <artifactId>spring-boot-starter-parent</artifactId>    <version>2.1.7.RELEASE</version>    <relativePath/> <!-- lookup parent from repository --></parent>

我的项目局部依赖

<dependency>    <groupId>org.springframework.boot</groupId>    <artifactId>spring-boot-starter-web</artifactId></dependency><dependency>    <groupId>org.springframework.boot</groupId>    <artifactId>spring-boot-starter-test</artifactId>    <scope>test</scope></dependency>

间接应用SpringBootTest形式

代码示例

@RunWith(SpringRunner.class)// 默认启动容器@SpringBootTestpublic class BdgResourceITest {    @Autowired    @Qualifier(value = "iTestRestTemplate")    private RestTemplate restTemplate;    @Test    public void testPull() throws URISyntaxException {        // /pull/{mof_div_code}/{fiscal_year}/{agency_code}        String url = "/api/pull/340000000/2022/001001";        final ResponseEntity<ResponseData> exchange = restTemplate.exchange(                RequestEntity.get(new URI(url)).build(), ResponseData.class);        Assert.isTrue(exchange.getStatusCode().equals(HttpStatus.OK), "本单位数据获取异样");    }}

场景及优劣

劣势

如果是测试类中大量引入了依赖, 这种状况下间接启动容器比拟不便, 不过集成测试个人感觉从入口拜访即可, 这种嵌套比拟深的倡议应用单元测试

劣势

以后我的项目中测试代码须要依赖很少, 极其状况下只用restTemplate即可, 基本没必要启动容器, 而且启动容器占用了大量工夫

我的项目中应用了ehcache3.x作为本地缓存, 启动容器后因为文件锁无奈测试, 如果独自指定ehcache.xml配置, 又会产生新的垃圾, 所以果决缩小依赖

最小化依赖计划

代码

@RunWith(SpringRunner.class)// 指定class就不启动容器了@SpringBootTest(classes = BdgResourceITest.class)@Import(value = {ITestRestTemplateConfigurer.class})// 激活 main 中resources下的test profile//@ActiveProfiles("dev")// 加载测试目录resources下的application.yml文件//@TestPropertySource(properties = {"spring.config.location=classpath:application.yml"})public class BdgResourceITest {    @Autowired    @Qualifier(value = "iTestRestTemplate")    private RestTemplate restTemplate;    @Test    public void testPull() throws URISyntaxException {        // /pull/{mof_div_code}/{fiscal_year}/{agency_code}        String url = "/api/pull/340000000/2022/001001";        final ResponseEntity<ResponseData> exchange = restTemplate.exchange(                RequestEntity.get(new URI(url)).build(), ResponseData.class);        Assert.isTrue(exchange.getStatusCode().equals(HttpStatus.OK), "本单位数据获取异样");    }}

思路及步骤

通过指定SpringBootTest的classes, 只启动以后类,如果须要注入其它bean, 则应用@import进行引入

如果import外部的类也也须要引入其它类, 同理依据须要应用@Import注解, 这样产生的代码更加聚合, 所然在以后类能够全副@Import, 然而看着头疼

对于须要引入yml配置信息的,能够配合@EnableConfigurationProperties读取测试目录下的application.yml文件

最小化依赖计划的长处

缩小了容器启动工夫, 对于以后我的项目更加符合实际的应用场景, 毕竟第三方应用不可能启动你本人的容器:D

更加优雅的解决了ehcache同时被容器扫描启动, 本地文件锁导致测试无奈运行, 理论测试代码基本不须要缓存, 我的项目服务有就行

测试代码也更加简略优雅, 能够间接提供第三方公司作为接口申请示例代码

论断

如果集成测试的场景相似以后我的项目状况, 全副测试都从rest接口动手, 倡议采纳最小容器依赖计划