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

执行mvn test时调用TestNG而非JUnit且无测试执行如何解决

问题原因

经过排查,你的问题由以下几个配置错误共同导致:

  • pom.xml语法错误:<properties>标签重复嵌套,导致全局配置解析异常
  • 缺失JUnit核心依赖:仅注释了JUnit的引入位置,未实际添加JUnit依赖,cucumber-junit需要依赖JUnit才能正常运行
  • maven-surefire-plugin配置位置错误:你将测试执行插件错误放在<dependencies>依赖节点下,插件必须放在<build><plugins>节点下才能生效,这直接导致你指定的2.22.2版本插件未加载,实际运行的是Maven默认携带的2.12.4版本旧插件,该版本的测试框架探测逻辑存在缺陷,会错误识别测试框架调用TestNG执行
  • 未明确指定测试运行框架,旧版插件探测优先级异常导致调用了非预期的TestNG

解决方案

按照以下步骤修改你的pom.xml即可:

  1. 修正嵌套错误的<properties>节点
  2. 新增JUnit核心依赖
  3. 将maven-surefire-plugin移动到<build><plugins>节点下,同时明确指定使用JUnit执行测试

修改后的完整pom.xml参考如下:

<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>你的groupId</groupId>
    <artifactId>APIframwork</artifactId>
    <version>0.0.1-SNAPSHOT</version>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>

    <dependencies>
        <!-- JUnit核心依赖 -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.13.2</version>
            <scope>test</scope>
        </dependency>

        <!-- https://mvnrepository.com/artifact/io.cucumber/cucumber-java -->
        <dependency>
            <groupId>io.cucumber</groupId>
            <artifactId>cucumber-java</artifactId>
            <version>6.9.1</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/io.cucumber/cucumber-junit -->
        <dependency>
            <groupId>io.cucumber</groupId>
            <artifactId>cucumber-junit</artifactId>
            <version>6.9.1</version>
            <scope>test</scope>
        </dependency>

        <!-- https://mvnrepository.com/artifact/io.rest-assured/rest-assured -->
        <dependency>
            <groupId>io.rest-assured</groupId>
            <artifactId>rest-assured</artifactId>
            <version>4.3.3</version>
            <scope>test</scope>
        </dependency>

        <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.12.3</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <!-- maven测试执行插件 -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.22.2</version>
                <configuration>
                    <!-- 明确指定使用JUnit执行测试 -->
                    <provider>junit</provider>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

修改完成后执行mvn clean test重新运行测试,即可看到TestNG相关输出消失,JUnit正常执行你的Cucumber测试用例。

内容的提问来源于stack exchange,提问作者user9534101

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.30 08:57:04