spring cloud + jenkins 运行单元测试并生成报告

本文介绍如何使用Maven进行多环境配置,包括开发、测试和生产环境,并详细说明了如何设置单元测试环境,禁用服务依赖,以及如何在Jenkins中运行单元测试。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1. 添加maven插件

<!-- 单元测试 -->
 <plugin>
     <groupId>org.apache.maven.plugins</groupId>
     <artifactId>maven-surefire-plugin</artifactId>
     <version>2.22.2</version>
 </plugin>

2. 在pom.xml中添加多环境配置

我们添加了专门跑单元测试的配置

<project>
    <profiles>
        <profile>
            <id>dev</id>
            <properties>
                <!-- 环境标识,需要与配置文件的名称相对应 -->
                <profiles.active>dev</profiles.active>
            </properties>
            <activation>
                <!-- 默认环境 -->
                <activeByDefault>true</activeByDefault>
            </activation>
        </profile>
        <profile>
            <id>test</id>
            <properties>
                <profiles.active>test</profiles.active>
            </properties>
        </profile>
        <profile>
            <id>prod</id>
            <properties>
                <profiles.active>prod</profiles.active>
            </properties>
        </profile>
        <profile>
            <id>devjunit</id>
            <properties>
                <profiles.active>devjunit</profiles.active>
            </properties>
        </profile>
        <profile>
            <id>testjunit</id>
            <properties>
                <profiles.active>testjunit</profiles.active>
            </properties>
        </profile>
    </profiles>
</project>

3. 修改bootstrap-devjunit.yml

这里我们需要把注册中心和配置中心都禁用掉,结合mock,可以避免服务间的相互依赖,影响测试结果。

spring:
  cloud:
    config:
      enabled: false
      discovery:
        enabled: false
  profiles:
    active: @profiles.active@
eureka:
  client:
    enabled: false

@profiles.active@ 和上一节中的配置对应,可以通过以下mvn命令指定环境:

mvn test -P devjunit

4. 替换yml文件中的变量

通过maven-resources-plugin插件,可以替换yml文件中的变量@profiles.active@。
在ide中运行服务时,可能会提示不合法的字符“@profiles.active@”,此时需要运行命令 mvn compile 。
该命令会通过以下插件把上面的变量替换为默认值“dev”,并拷贝到target目录中。

<plugin>
   <groupId>org.apache.maven.plugins</groupId>
     <artifactId>maven-resources-plugin</artifactId>
     <version>3.1.0</version>
     <executions>
         <execution>
             <id>default-resources</id>
             <phase>validate</phase>
             <goals>
                 <goal>copy-resources</goal>
             </goals>
             <configuration>
                 <outputDirectory>target/classes</outputDirectory>
                 <useDefaultDelimiters>false</useDefaultDelimiters>
                 <delimiters>
                     <delimiter>@</delimiter>
                 </delimiters>
                 <resources>
                     <resource>
                         <directory>src/main/resources/</directory>
                         <filtering>true</filtering>
                         <includes>
                             <include>**/*.yml</include>
                             <include>**/*.xml</include>
                             <include>**/*.mmdb</include>
                         </includes>

                     </resource>
                 </resources>
                 <!-- 不替换下面扩展名的文件 -->
                 <nonFilteredFileExtensions>
                     <nonFilteredFileExtension>mmdb</nonFilteredFileExtension>
                 </nonFilteredFileExtensions>
             </configuration>
         </execution>
     </executions>
 </plugin>

5 在jenkins中运行单元测试

在jenkins中添加以下步骤:
在这里插入图片描述

6 报告分析配置

在jenkins中安装插件"Warnings Next Generation",

添加一个“Post-build Actions”:Record compiler warnings and static analysis results, 选择junit:

在这里插入图片描述

完成。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

xiegwei

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值