【问题标题】:Maven compile error [package org.testng.asserts does not exist]Maven编译报错【包org.testng.asserts不存在】
【发布时间】:2023-04-11 10:56:01
【问题描述】:

当我尝试通过 [mvn clean install -DskipTests] 通过控制台构建项目时出现错误。我在测试中使用 testNG SoftAssert,在测试类中我刚刚添加了一个 import import org.testng.asserts.SoftAssert 但看起来 maven 没有看到那个包。 来自控制台的错误:

包 org.testng.asserts 不存在

我的 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>com.atlassian</groupId>
<artifactId>tests</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>confluence</name>
<url>http://maven.apache.org</url>
<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
    <dependency>
        <groupId>org.testng</groupId>
        <artifactId>testng</artifactId>
        <version>6.1.1</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-java</artifactId>
        <version>2.48.2</version>
    </dependency>
    <dependency>
        <groupId>log4j</groupId>
        <artifactId>log4j</artifactId>
        <version>1.2.17</version>
    </dependency>
</dependencies>
<build>
    <plugins>
        <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.1</version>
            <configuration>
                <source>1.7</source>
                <target>1.7</target>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.16</version>
            <configuration>
                <suiteXmlFiles>
                    <suiteXmlFile>testng.xml</suiteXmlFile>
                </suiteXmlFiles>
            </configuration>
        </plugin>
    </plugins>
</build>

【问题讨论】:

  • maven 是否在识别其他 TestNG 类?就像你使用@Test时一样,它是否会给出类似的错误?

标签: maven testng


【解决方案1】:

当相应的依赖版本没有您尝试使用的类时,会发生此类错误。在这种情况下,您使用的TestNG version 6.1.1 没有包org.testng.asserts。尝试使用以下版本, 此外,如果您已要求 IDE 包含 TestNG library,则导入 SoftAsserts 不会出错。这个TestNG 库的版本肯定比您从pom.xml 引用的版本更高。尝试在 pom.xml 和您的 IDE 的 testNG plugin 中保持相同的版本以避免这种不同的行为。

 <dependency>
    <groupId>org.testng</groupId>
    <artifactId>testng</artifactId>
    <version>6.8.8</version>
 </dependency>

以上版本肯定可以工作。试试看吧。

【讨论】:

    【解决方案2】:

    我发现删除 testng 依赖项中的范围是有效的。我尝试在添加到相同依赖项的范围内运行,但失败了。奇怪,但它只是通过删除 testng 范围依赖来工作。

    【讨论】:

      【解决方案3】:

      尝试了不同的版本,但没有帮助。从依赖项中删除范围确实解决了这个问题。

      【讨论】:

        猜你喜欢
        • 2014-01-29
        • 1970-01-01
        • 2017-08-30
        • 2019-01-21
        • 2019-12-02
        • 2013-12-19
        • 2013-05-31
        • 1970-01-01
        • 2013-06-19
        相关资源
        最近更新 更多