java自动化测试框架第四篇-如何获取config中的数据

一、获取config.properties中的数据

1. 在resources目录下配置config.properties文件

# 人才库入职

talentPool.fullSpell = autotest

talentPool.idCardEndDate = 2020-04-29

talentPool.baseRegionCode = MRYXHB

2. 添加注解:在LopApplication添加注解

@PropertySource("classpath:config.properties")
public class LopApplication {

  public static void main(String[] args) {
    SpringApplication.run(LopApplication.class, args);
  }
}

3. 在用例中引用数据

/ 调用录用员工接口
EmployNormalBody body = new EmployNormalBody();

body.setLabourName(environment.getProperty("talentPool.labourName"));
body.setBaseStationCode(environment.getProperty("talentPool.baseStationCode"));

Response res = context.getBean(EmployNormal.class).setBody(body).request();

Assert.assertEquals(res.getStatusCode(), 200, "人才库-录用人员,返回状态码!=200");

二、读取config.properties乱码问题

1. 当配置文件为中文的时候,会出现乱码的问题

image

2. 解决方式:在application类中的注解后添加encoding = "utf-8"

@PropertySource(value = "classpath:config.properties", encoding = "utf-8")
public class LopApplication {
    ...
}
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。