You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

新增依赖后Maven项目API测试失败问题排查求助

Maven项目新增依赖后API测试失败及SLF4J/反射警告解决指南

问题概述

新增依赖后,Selenium UI测试正常执行,但API测试失败,同时触发两类核心警告,附加TestNG静态方法警告:

  1. SLF4J无实现警告
  2. Groovy非法反射访问警告
  3. TestNG静态配置方法警告

警告详情

SLF4J警告

SLF4J: No SLF4J providers were found.
SLF4J: Defaulting to no-operation (NOP) logger implementation.
SLF4J: See https://www.slf4j.org/codes.html#noProviders for further details.

原因:项目依赖中仅引入SLF4J API,但未提供具体的日志实现(如Logback、Log4j2)。

Groovy反射警告

WARNING: An illegal reflective access operation has occurred.
WARNING: Illegal reflective access by org.codehaus.groovy.vmplugin.v9.Java9 (file:/Users/bariscanates/.m2/repository/org/apache/groovy/groovy/4.0.11/groovy-4.0.11.jar) to constructor java.lang.AssertionError(java.lang.String).
WARNING: Please consider reporting this to the maintainers of org.codehaus.groovy.vmplugin.v9.Java9.
WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations.
WARNING: All illegal access operations will be denied in a future release.

原因:引入的依赖(如旧版Unirest)携带的Groovy版本与Java 11的反射权限不兼容,Groovy尝试访问受限的Java API。

TestNG静态方法警告

[main] WARN org.testng.internal.Configuration - Detected a static method [api.Tests.TrelloChainApiTest.TrelloTestRestAssured.setup()]. Static configuration methods can cause unexpected behavior.

原因:TestNG的静态配置方法会导致测试上下文共享,引发并行测试状态混乱等问题。

已尝试无效方案

  • 执行mvn clean、mvn clean install、mvn dependency:resolve、mvn dependency:tree清理缓存
  • 移除并重新添加新依赖

依赖冲突分析(基于提供的pom.xml)

  1. Gson版本冲突:同时引入2.9.0和2.10.1两个版本,可能导致JSON序列化/反序列化逻辑不一致
  2. JUnit生态冲突:同时存在JUnit 4、JUnit Jupiter(JUnit 5)和TestNG,测试框架依赖混乱
  3. Unirest版本过旧:com.mashape.unirest:unirest-java:1.4.9是停止维护的旧版本,依赖的Groovy版本存在Java 11兼容性问题
  4. SLF4J缺失实现:所有依赖SLF4J的组件(如RestAssured、Unirest)均未绑定具体日志实现

分步解决指南

1. 解决SLF4J警告

添加SLF4J的具体实现,推荐使用Logback(轻量且常用):

<dependency>
    <groupId>ch.qos.logback</groupId>
    <artifactId>logback-classic</artifactId>
    <version>1.4.11</version>
    <scope>test</scope>
</dependency>

2. 解决Groovy反射警告

替换旧版Unirest为维护中的最新版本(已迁移到com.konghqgroupId),新版本修复了Java 11兼容性问题:

<!-- 移除旧版Unirest -->
<!--<dependency>
    <groupId>com.mashape.unirest</groupId>
    <artifactId>unirest-java</artifactId>
    <version>1.4.9</version>
</dependency>-->

<!-- 添加新版Unirest -->
<dependency>
    <groupId>com.konghq</groupId>
    <artifactId>unirest-java</artifactId>
    <version>4.2.2</version>
</dependency>

若仍存在Groovy依赖问题,可通过mvn dependency:tree定位引入Groovy的依赖,排除旧版本:

<!-- 示例:排除RestAssured中的Groovy依赖 -->
<dependency>
    <groupId>io.rest-assured</groupId>
    <artifactId>rest-assured</artifactId>
    <version>5.3.2</version>
    <scope>test</scope>
    <exclusions>
        <exclusion>
            <groupId>org.apache.groovy</groupId>
            <artifactId>groovy</artifactId>
        </exclusion>
    </exclusions>
</dependency>

3. 解决依赖冲突

  • 统一Gson版本:移除2.9.0的Gson依赖,保留2.10.1(更高版本兼容性更好)
  • 清理JUnit依赖:若主要使用TestNG,可移除JUnit 4和JUnit Jupiter依赖;若需兼容,将JUnit Jupiter的scope改为test并指定具体版本:
<dependency>
    <groupId>org.junit.jupiter</groupId>
    <artifactId>junit-jupiter</artifactId>
    <version>5.10.0</version>
    <scope>test</scope>
</dependency>

4. 解决TestNG静态方法警告

将TrelloTestRestAssured.setup()改为非静态方法,TestNG会自动实例化测试类并调用非静态配置方法,避免状态共享问题:

// 原静态方法
// public static void setup() { ... }

// 修改为非静态方法
public void setup() { ... }

验证步骤

  1. 执行mvn clean test清理并运行测试
  2. 检查控制台是否仍有警告
  3. 确认API测试是否正常执行

内容的提问来源于stack exchange,提问作者Barış Can Ateş

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.15 02:03:09