Maven编译错误[package org.testng.asserts不存在]

时间:2015-12-03 01:36:16

标签: maven testng

当我尝试通过[mvn clean install -DskipTests]通过控制台构建项目时,我收到错误。我在我的测试中使用testNG SoftAssert,在测试类中我刚添加了一个导入导入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>

2 个答案:

答案 0 :(得分:0)

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

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

以上版本肯定有效。试一试。

答案 1 :(得分:0)

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

相关问题