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

3831956b6f0e82aa0614841e150a73a7.png

前言

但在大部分用例开发环境下,添加额外配置是无所避免的,比如自定义应用端口号、服务地址、数据库的配置等,都或多或少的需要一些外部的配置项等。

在前文中我们有详细介绍在接口测试框架中如何基于 SpringBoot 快速搭建多环境配置,本文将在原有的基础上介绍集成如何快速读取配置文件的值。

配置文件简要说明

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

Demo 演示

这次在多环境配置的 demo 的基础进行扩展。

自定义属性值

filter-dev.properties 配置文件增加自定义属性,比如:

host=http://127.0.0.1

port=8082

application-dev.properties 增加配置项:

Server.host=${host}

Server.port=${port}

新建配置实体类

我们可以通过两种方式配置绑定对象。

第一种方式:@Value() 方式 在类域属性上通过 @Value("${xxx}") 指定关联属性, SpringBoot 会自动加载。@Component 注解使其在启动时被自动扫描到。

packagecom.zuozewei.springboot.model;

importlombok.Data;

importorg.springframework.beans.factory.annotation.Value;

importorg.springframework.stereotype.Component;

/**

* 描述:

* 配置文件实体类

*

* @author zuozewei

* @create 2019-12-20 16:15

*/

@Component

@Data

publicclassConfigurations1{

@Value("${Server.host}")

privateStringhost;

@Value("${Server.port}")

privateStringport;

}

第二种方式:@ConfigurationProperties 属性

手动书写 @Value 注解还是比较繁重的工作,好在 SpringBoot 提供了更简洁的方式。@ConfigurationProperties(prefix = "Server")。prefix 指定了配置文件的前缀为 Server,并且按照属性名进行自动匹配。例如:Server.host属性值会自动加载到 privateStringhost 域中。

packagecom.zuozewei.springboot.model;

importlombok.Data;

importorg.springframework.boot.context.properties.ConfigurationProperties;

importorg.springframework.stereotype.Component;

/**

* 描述:

* 配置文件实体类

*

* @author zuozewei

* @create 2019-12-20 16:15

*/

@Component

@Data

@ConfigurationProperties(prefix ="Server")

publicclassConfigurations2{

privateStringhost;

privateStringport;

}

PS:locations 还能够指定自定义的配置文件位置,这里就不多说了。

@ConfigurationProperties(prefix ="Server", locations ="classpath:xxxx.properties")

用例读取

编写测试用例,通过 @Autowired 注解注入 bean 调用。

packagecom.zuozewei.springboot.test;

importcom.zuozewei.springboot.model.Configurations1;

importlombok.extern.slf4j.Slf4j;

importorg.springframework.beans.factory.annotation.Autowired;

importorg.springframework.boot.test.context.SpringBootTest;

importorg.springframework.test.context.testng.AbstractTestNGSpringContextTests;

importorg.testng.annotations.BeforeClass;

importorg.testng.annotations.Test;

/**

* 描述:

* 演示测试用例1

*

* @author zuozewei

* @create 2020-01-03 11:02

*/

@SpringBootTest

@Slf4j

publicclassTestCase1extendsAbstractTestNGSpringContextTests{

@Autowired

privateConfigurations1configurations;

@BeforeClass

publicvoidbeforeClass() {

Stringhost = configurations.getHost();

Stringport = configurations.getPort();

Stringurl = host +":"+ port;

log.info("URL:"+ url );

}

@Test

publicvoidtest(){

log.info("TestCase run...");

}

}

注意:

SpringBoot 中读取配置文件不能放到 @BeforeSuite 注解,否则会导致 @Autowired 不能加载 Bean;

SpringBoot 中使用 TestNg 必须加上 @SpringBootTest,并且继承 AbstractTestNGSpringContextTests,如果不继承 AbstractTestNGSpringContextTests,会导致 @Autowired 不能加载 Bean。

测试验证

最好跑测看下结果,我们可以看到配置文件读取成功:

773c28df45f1c8ef35ba3a7c83fb675f.png

小结

测试框架使用 SpingBoot 读取配置文件比我们传统方式要简单很多,上述我们主要介绍了过两种方式配置绑定对象:

@Value() 注解

@ConfigurationProperties 属性 最后在测试用例开发中,结合 @Autowired 注解注入 bean 调用读取即可。

希望本文对你有所启发。

示例代码:

https://github.com/7DGroup/Java-API-Test-Examples/tree/master/springboot-configuration-demo

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值