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

使用TestNG运行测试时出现模块访问权限及版本解析异常

TestNG运行测试报错及解决方案

错误日志

[RemoteTestNG] 无法从类获取TestNG版本:java.lang.IllegalArgumentException: 无效版本"[WORKING]":非数值"[WORKING]"
[RemoteTestNG] 尝试从pom.properties解析版本
[RemoteTestNG] 尝试从MANIFEST.MF解析版本
[RemoteTestNG] 从jar:file:///C:/Users/Shreyas%20Lembhe/Downloads/testng-7.10.2.jar!/META-INF/MANIFEST.MF解析TestNG版本
[RemoteTestNG] 检测到TestNG版本7.10.2
SLF4J: 无法加载类"org.slf4j.impl.StaticLoggerBinder"。
SLF4J: 默认使用无操作(NOP)日志实现
SLF4J: 详情请见相关文档。
org.testng.TestNGException:
实例化test.SumValidation类时出错:无法使public test.SumValidation()可访问:模块DemoProject未将test包导出给模块org.testng
(后续堆栈跟踪省略)

项目结构

  • src文件夹
    • files包:Payload.java、ReUsableMethods.java
    • test包:Basics.java、ComplexJsonParse.java、SumValidation.java

依赖配置情况

已通过两种方式添加testng、restAssured和hamcrest依赖:

  1. Maven依赖(配置pom.xml,也尝试过手动下载添加)
  2. 项目库(通过Build Path->Libraries->ModulePath->添加外部JARs)

代码文件

SumValidation.java(test包下)

package test;

import org.testng.annotations.Test;

import files.Payload;
import io.restassured.path.json.JsonPath;

public class SumValidation {

    @Test
    public void sumOfCourses() {
        JsonPath js=new JsonPath(Payload.coursePrice());
        int count=js.getInt("courses.size()");
        
        for(int i=0;i<count;i++) {
            int price=js.getInt("count["+i+"].price");
            int copies=js.getInt("count["+i+"].copies");
            int amount=price*copies;
            System.out.println(amount);
        }
    }
}

Payload.java(files包下)

package files;

public class Payload {

    public static String addPlace() {
        return "{\r\n"
                + "  \"location\": {\r\n"
                + "    \"lat\": -38.383494,\r\n"
                + "    \"lng\": 33.427362\r\n"
                + "  },\r\n"
                + "  \"accuracy\": 50,\r\n"
                + "  \"name\": \"Frontline house\",\r\n"
                + "  \"phone_number\": \"(+91) 983 893 3937\",\r\n"
                + "  \"address\": \"29, side layout, cohen 09\",\r\n"
                + "  \"types\": [\r\n"
                + "    \"shoe park\",\r\n"
                + "    \"shop\"\r\n"
                + "  ],\r\n"
                + "  \"website\": \"http://google.com\",\r\n"
                + "  \"language\": \"French-IN\"\r\n"
                + "}\r\n"
                + "";
    }

    public static String coursePrice() {
        return "{\r\n"
                + "\r\n"
                + "\"dashboard\": {\r\n"
                + "\r\n"
                + "\"purchaseAmount\": 910,\r\n"
                + "\r\n"
                + "\"website\": \"rahulshettyacademy.com\"\r\n"
                + "\r\n"
                + "},\r\n"
                + "\r\n"
                + "\"courses\": [\r\n"
                + "\r\n"
                + "{\r\n"
                + "\r\n"
                + "\"title\": \"Selenium Python\",\r\n"
                + "\r\n"
                + "\"price\": 50,\r\n"
                + "\r\n"
                + "\"copies\": 6\r\n"
                + "\r\n"
                + "},\r\n"
                + "\r\n"
                + "{\r\n"
                + "\r\n"
                + "\"title\": \"Cypress\",\r\n"
                + "\r\n"
                + "\"price\": 40,\r\n"
                + "\r\n"
                + "\"copies\": 4\r\n"
                + "\r\n"
                + "},\r\n"
                + "\r\n"
                + "{\r\n"
                + "\r\n"
                + "\"title\": \"RPA\",\r\n"
                + "\r\n"
                + "\"price\": 45,\r\n"
                + "\r\n"
                + "\"copies\": 10\r\n"
                + "\r\n"
                + "},\r\n"
                + "\r\n"
                + "{\r\n"
                + "\r\n"
                + "\"title\": \"Appium\",\r\n"
                + "\r\n"
                + "\"price\": 55,\r\n"
                + "\r\n"
                + "\"copies\": 15\r\n"
                + "\r\n"
                + "}\r\n"
                + "\r\n"
                + "\r\n"
                + "]\r\n"
                + "\r\n"
                + "}";
    }
}

pom.xml内容

<?xml version="1.0" encoding="UTF-8"?>
<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>DemoProject</groupId>
    <artifactId>DemoProject</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <build>
        <sourceDirectory>src</sourceDirectory>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <configuration>
                    <release>22</release>
                </configuration>
            </plugin>
        </plugins>
    </build>
    <dependencies>
        <!-- https://mvnrepository.com/artifact/io.rest-assured/rest-assured -->
        <dependency>
            <groupId>io.rest-assured</groupId>
            <artifactId>rest-assured</artifactId>
            <version>5.5.0</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.testng/testng -->
        <dependency>
            <groupId>org.testng</groupId>
            <artifactId>testng</artifactId>
            <version>7.10.2</version>
            <scope>test</scope>
        </dependency>
        
        <!-- https://mvnrepository.com/artifact/org.testng/testng -->
        <dependency>
            <groupId>org.testng</groupId>
            <artifactId>testng</artifactId>
            <version>7.10.2</version>
            <scope>test</scope>
        </dependency>


        <!-- https://mvnrepository.com/artifact/org.hamcrest/hamcrest -->
        <dependency>
            <groupId>org.hamcrest</groupId>
            <artifactId>hamcrest</artifactId>
            <version>3.0</version>
            <scope>test</scope>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.jcommander/jcommander -->
        <dependency>
            <groupId>org.jcommander</groupId>
            <artifactId>jcommander</artifactId>
            <version>2.0</version>
        </dependency>


    </dependencies>
</project>

解决方案

1. 解决模块访问权限问题

错误核心是模块DemoProject未将test包导出给org.testng,处理方式二选一:

  • 若不需要Java模块系统,直接删除项目根目录下的module-info.java文件;
  • 若保留模块系统,在module-info.java中添加导出声明:
    module DemoProject {
        exports test to org.testng;
        exports files; // 若其他模块需访问files包也需导出
    }
    

2. 修复pom.xml问题

  • 删除重复的TestNG依赖,仅保留一个;
  • 添加SLF4J日志实现依赖,消除警告,例如添加logback-classic:
    <dependency>
        <groupId>ch.qos.logback</groupId>
        <artifactId>logback-classic</artifactId>
        <version>1.4.8</version>
        <scope>test</scope>
    </dependency>
    

3. 修复代码逻辑错误

SumValidation.java中循环里的count["+i+"]应为courses["+i+"](对应JSON中的数组字段),修正后代码:

for(int i=0;i<count;i++) {
    int price=js.getInt("courses["+i+"].price");
    int copies=js.getInt("courses["+i+"].copies");
    int amount=price*copies;
    System.out.println(amount);
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.18 23:59:58