0
点赞
收藏
分享

微信扫一扫

走进Java接口测试之多环境配置

米小格儿 2023-04-03 阅读 95

走进Java接口测试之多环境配置_bc


背景

在日常开发过程中,我们都会有多套开发环境,比如:开发、测试、生产等不同的应用环境,这些应用环境都对应不同的配置项,包括不同环境数据库地址、端口号等都是不尽相同的,要是没有多环境的自由切换,部署起来是很繁琐也容易出错的。本文主要介绍在 SpringBoot 项目中如何进行多环境配置的方法。

采用Maven环境切换方式

在 Maven项目中,我们有一种简洁的多环境配置方式,Maven 的思路是资源文件根据环境进行隔离,在测试的时候去加载正确的配置资源进行配置,另外 Maven 的多环境资源隔离配置与 Jenkins CI 集成较好。

pom.xml配置

先配置 pom.xml 文件的 build 节点。

    <build>
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <filtering>true</filtering>
                <!--扫描替换参数的文件路径-->
            </resource>
        </resources>
        <filters>
            <filter>src/main/filters/filter-${env}.properties</filter>
            <!--环境过滤器的配置方式,回头需要在该路径下建立对应文件-->
        </filters>

        <plugins>
            <plugin>
                <artifactId>maven-resources-plugin</artifactId>
                <configuration>
                    <encoding>${project.build.sourceEncoding}</encoding>
                    <!-- 需要加入,因为maven默认的是${},而springbooot 默认会把此替换成@{} -->
                    <useDefaultDelimiters>true</useDefaultDelimiters>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>${spring-boot-maven-plugin.version}</version>
                <configuration>
                    <fork>true</fork>
                </configuration>
            </plugin>
        </plugins>
    </build>

在 pom.xml 文件配置 properties

     <!-- 主要依赖库的版本定义,可以采用${属性名}引用 -->
 <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>

        <!-- plugin 定义 -->
        <spring-boot-maven-plugin.version>2.1.6.RELEASE</spring-boot-maven-plugin.version>
</properties>

以上的配置主要做的事情就是标记资源文件,把 src/main/filters/filter-${env}.properties 也标记为了资源文件,{env} 的具体值见下面的配置

在 pom.xml 文件配置 Properties 环境,多环境配置参数切换

 <!-- 不同的测试环境 -->
    <profiles>
        <!-- 开发环境,默认激活 -->
        <profile>
            <id>dev</id>
            <properties>
                <env>dev</env>
            </properties>
            <activation>
                <activeByDefault>true</activeByDefault><!--默认启用环境配置-->
            </activation>
        </profile>

        <!-- 测试环境 -->
        <profile>
            <id>test</id>
            <properties>
                <env>test</env>
            </properties>
        </profile>

        <!-- 线上环境 -->
        <profile>
            <id>product</id>
            <properties>
                <env>product</env>
            </properties>
        </profile>
    </profiles>

配置文件

在 src/main/filters 下创建配置文件:

filter-dev.properties:开发环境

filter-product.properties:线上环境

filter-test.properties:测试环境

用于环境信息记录,如:

#Environment


Environment=dev


host=http://127.0.0.1

port=8082


jdbc-url=xxxx

jdbc-username=xxxx

jdbc-password=xxxx

在 src/resource 下创建 application-maven.properties 文件。该文件记录的信息是跟环境切换相关的参数,里面可以使用 key=value 的形式配置变量。如:接口请求不同环境的host、数据库等,因不同环境的信息。

server.port=${port}


# Environment

Environment=${Environment}

Host.url=${host}


# 数据源配置

spring.datasource.url=${jdbc-url}

spring.datasource.username==${jdbc-usernamel}

spring.datasource.password==${jdbc-password}

spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver

如图:

走进Java接口测试之多环境配置_bc_02

然后编译时,maven 命令加入参数 -P 命令即可指定相应的环境资源,比如: -Ptest,则会替换 test 环境下的参数值。

mvn clean install -DskipTests -Ptest

Jenkins 配置

在 Jenkins 使用 Maven 构建项目测试前,先通过本地使用 maven 测试是否通过。这里本来要将参数化构建,但参数化构建前先说明下是如何利用 maven 构建测试的。 走进Java接口测试之多环境配置_maven_03同样,env 对应 maven 构建中的 -P%env% ,再对应 pom.xml 中的build信息,加入运行的环境选项

如下:

clean test -U -DxmlFileName=%xmlFileName% -P%env%

走进Java接口测试之多环境配置_maven_04

springboot多环境配置

Profile 是 Spring 针对不同环境不同配置的支持。需要满足 application-{profile}.properties,{profile} 对应你的环境标识。如:

application-dev.properties:开发环境

application-test.properties:测试环境

application-product.properties:线上环境

在不同环境的配置文件中使用 key=value 的形式配置变量。

server.port=8081

而指定执行哪份配置文件,只需要在 application.properties 配置 spring.profiles.active 为对应 ${profile} 的值。

# 指定环境为dev

spring.profiles.active=dev

则会加载 application-dev.properties 的配置内容 走进Java接口测试之多环境配置_maven_05

小结

一般我们在做自动化测试集成执行的时候,推荐 Maven 环境切换方式,因为可以做到动态环境切换, 而 springboot 多环境配置在使用IDE开发的时候使用比较方便。


本文源码:

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

举报

相关推荐

0 条评论