走进Java接口测试之读取配置文件

发表于:2022-4-02 09:52

字体: | 上一篇 | 下一篇 | 我要投稿

 作者:zuozewei    来源:稀土掘金

  一、前言
  在大部分用例开发环境下,添加额外配置是无所避免的,比如自定义应用端口号、服务地址、数据库的配置等,都或多或少的需要一些外部的配置项等。本文将介绍集成如何快速读取配置文件的值。

  二、配置文件简要说明
  SpringBoot 默认的全局配置文件名为 application.properties 或 application.yml (spring官方推荐使用的格式是 .yml 格式),程序启动时会自动加载此文件,无需手动引入。
  除此之外还有一个 bootstrap 的全局文件,它是在 application 配置文件之前加载,主要是用于在应用程序上下文的引导阶段,在后 SpringCloud时,主要是利用此特性,进行配置文件的动态修改,在此 我们演示application.properties 配置。

  三、Demo 演示
  这次在多环境配置的 demo 的基础进行扩展。
  1、自定义属性值
  filter-dev.properties 配置文件增加自定义属性,比如:
  host=http://127.0.0.1
  port=8082
  
  application-dev.properties 增加配置项:
  Server.host=${host}
  Server.port=${port}
  
  2、新建配置实体类
  我们可以通过两种方式配置绑定对象。
  第一种方式:@Value() 方式。
  在类域属性上通过 @Value("${xxx}") 指定关联属性, SpringBoot 会自动加载。
  @Component 注解使其在启动时被自动扫描到。
  package com.zuozewei.springboot.model;
  import lombok.Data;
  import org.springframework.beans.factory.annotation.Value;
  import org.springframework.stereotype.Component;
  /**
   * 描述:
   * 配置文件实体类
   *
   * @author zuozewei
   * @create 2019-12-20 16:15
   */
  @Component
  @Data
  public class Configurations1 {
      @Value("${Server.host}")
      private String host;
      @Value("${Server.port}")
      private String port;
  }
  
  第二种方式:@ConfigurationProperties 属性。手动书写 @Value 注解还是比较繁重的工作,好在 SpringBoot 提供了更简洁的方式。@ConfigurationProperties(prefix = "Server")。prefix 指定了配置文件的前缀为 Server,并且按照属性名进行自动匹配。
  例如:Server.host属性值会自动加载到 private String host 域中。
  package com.zuozewei.springboot.model;
  import lombok.Data;
  import org.springframework.boot.context.properties.ConfigurationProperties;
  import org.springframework.stereotype.Component;
  /**
   * 描述:
   * 配置文件实体类
   *
   * @author zuozewei
   * @create 2019-12-20 16:15
   */
  @Component
  @Data
  @ConfigurationProperties(prefix = "Server")
  public class Configurations2 {
      private String host;
      private String port;
  }

  PS:locations 还能够指定自定义的配置文件位置,这里就不多说了。
  @ConfigurationProperties(prefix = "Server", locations = "classpath:xxxx.properties")
 
  3、用例读取
  编写测试用例,通过 @Autowired 注解注入 bean 调用。
  package com.zuozewei.springboot.test;
  import com.zuozewei.springboot.model.Configurations1;
  import lombok.extern.slf4j.Slf4j;
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.boot.test.context.SpringBootTest;
  import org.springframework.test.context.testng.AbstractTestNGSpringContextTests;
  import org.testng.annotations.BeforeClass;
  import org.testng.annotations.Test;
  /**
   * 描述:
   * 演示测试用例1
   *
   * @author zuozewei
   * @create 2020-01-03 11:02
   */
  @SpringBootTest
  @Slf4j
  public class TestCase1 extends AbstractTestNGSpringContextTests {
      @Autowired
      private Configurations1 configurations;
      @BeforeClass
      public void beforeClass() {
          String host = configurations.getHost();
          String port = configurations.getPort();
          String url = host +":"+ port;
          log.info("URL:" + url );
      }
      @Test
      public void test(){
          log.info("TestCase run...");
      }
  }

  注意:
  ·SpringBoot 中读取配置文件不能放到 @BeforeSuite 注解,否则会导致 @Autowired 不能加载 Bean;
  · SpringBoot 中使用 TestNg 必须加上 @SpringBootTest,并且继承 AbstractTestNGSpringContextTests,如果不继承AbstractTestNGSpringContextTests,会导致 @Autowired 不能加载 Bean。

  4、测试验证
  最好跑测看下结果,我们可以看到配置文件读取成功:
  四、小结
  测试框架使用 SpingBoot 读取配置文件比我们传统方式要简单很多,上述我们主要介绍了过两种方式配置绑定对象:
  @Value() 注解;
  @ConfigurationProperties 属性;
  最后在测试用例开发中,结合@Autowired 注解注入 bean 调用读取即可。
  希望本文对你有所启发。

  本文内容不用于商业目的,如涉及知识产权问题,请权利人联系51Testing小编(021-64471599-8017),我们将立即处理
《2023软件测试行业现状调查报告》独家发布~

关注51Testing

联系我们

快捷面板 站点地图 联系我们 广告服务 关于我们 站长统计 发展历程

法律顾问:上海兰迪律师事务所 项棋律师
版权所有 上海博为峰软件技术股份有限公司 Copyright©51testing.com 2003-2024
投诉及意见反馈:webmaster@51testing.com; 业务联系:service@51testing.com 021-64471599-8017

沪ICP备05003035号

沪公网安备 31010102002173号