Maven编译错误【包org.testng.annotations不存在】。

27
我对Maven不太熟悉,想用它来运行我的测试类。我已经生成了testng.xml文件,并且也创建了pom.xml文件。但是当我运行“mvn install”命令时,会出现以下错误:

[package org.testng.annotations does not exist]

请你指导我该怎么办。

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.TestNG</groupId>
    <artifactId>TestNG</artifactId>
    <version>0.0.1-SNAPSHOT</version>

    <dependencies>
        <dependency>
            <groupId>org.testng</groupId>
            <artifactId>testng</artifactId>
            <version>6.1.1</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>

        <sourceDirectory>src</sourceDirectory>

        <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>
</project>

testng.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite" verbose="1" preserve-order="true">

    <test name="Test">
        <packages>
            <package name="com.testngTest2" />
            <package name="com.testngTest" />
        </packages>
    </test> <!-- Test -->
</suite> <!-- Suite -->

1
你的项目中的测试类在哪里?例如在src/test/java下还是其他地方? - DB5
10个回答

73

我遇到了类似的问题。问题出在“testng”依赖项的“Scope”选项设置成了“test”,而需要的是“compile”。

以下是我是如何解决它的(注意,我使用的是Eclipse):

  1. 打开pom.xml文件。
  2. 进入Dependencies选项卡。
  3. 选择"testng"包并单击“属性...
  4. 在弹出的屏幕上将“Scope”选项更改为“compile”,然后单击“确定”以保存更改。
  5. 再次尝试使用"compile test"目标构建您的项目。

谢谢Pavik,"编译测试"对我有用,但是我在清理安装时遇到了错误。 - Shubham Jain
对于IntelliJ,转到文件>项目结构>模块,进入注释处理模块,并将范围从测试更改为编译。这对我也起作用了,我只是为IntelliJ用户添加了这个评论。 - Nav12

29

移除测试范围的TestNG依赖并添加编译依赖。

<dependency>
        <groupId>org.testng</groupId>
        <artifactId>testng</artifactId>
        <version>6.1.1</version>
        <scope>compile</scope>
    </dependency>

16
在你的 pom.xml 文件中,testng 的范围为 test
<dependency>
            <groupId>org.testng</groupId>
            <artifactId>testng</artifactId>
            <version>6.9.13.6</version>
            <scope>test</scope>
</dependency>

编译替换作用域(scope)

<dependency>
            <groupId>org.testng</groupId>
            <artifactId>testng</artifactId>
            <version>6.9.13.6</version>
            <scope>compile</scope>
</dependency>

7
Beleive me below wil work replace "test" to "compile" in scope
<dependency>
            <groupId>org.testng</groupId>
            <artifactId>testng</artifactId>
            <version>6.9.13.6</version>
            <scope>compile</scope>
</dependency>

1
为什么?为什么?为什么它能工作?它对我有效,但不知道为什么。 - Pramod

5

我曾经遇到过这个问题,并找到了解决方法。

在您的pom.xml文件中,移除作用域(scope)标签,就可以正常工作了。

根据以下代码在pom.xml中,删除scope标签。

即使我们已经导入了Testng的maven依赖项,在XML文件中添加了scope标签后,它会被视为JUnit注释而不是Testng。所以当我删除scope标签时,我的@Test注释被视为Testng注释。

<dependency>
            <groupId>org.testng</groupId>
            <artifactId>testng</artifactId>
            <version>6.8.7</version>
            **<scope>test</scope>** //Remove this line and compile maven
</dependency>

2
    <dependency>
        <groupId>org.testng</groupId>
        <artifactId>testng</artifactId>
        <version>6.8.8</version>
        <scope>compile</scope>
    </dependency>

1
确保你的测试代码在 "src/test/java" 目录下,这样可以解决这个问题。

0

如果你正在构建一个 Web 应用程序并且不想将其包含在 WEB-INF/lib 中,请将 testng 的范围设置为 "provided"。


0

对于无法找到pom.xml文件的JetBrains IntelliJ用户:

  1. 进入“文件”>“项目结构”
  2. 从“项目设置”>“模块”中选择测试包所在的模块
  3. 进入依赖项选项卡
  4. 将范围设置从“测试”更改为“编译”

0
以下解决方案适用于我 -

请在您的pom.xml中添加以下依赖项

<dependency>
    <groupId>org.testng</groupId>
    <artifactId>testng</artifactId>
    <version>7.0.0</version>
    <scope>test</scope>
</dependency>

<dependency>
    <groupId>com.google.guava</groupId>
    <artifactId>guava</artifactId>
    <version>17.0</version>
</dependency>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接