27

I'm pretty new to maven and I want to run my test classes using maven. I have generated the testng.xml and I have created the POM.xml file also. But when you run the mvn install, it generates this error :

[package org.testng.annotations does not exist]

please advice on this.

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 -->
Manmohan_singh
  • 1,776
  • 3
  • 20
  • 29
user3060386
  • 271
  • 1
  • 3
  • 5
  • 1
    where in your project are your test classes? i.e. under src/test/java or somewhere else? – DB5 Dec 03 '13 at 08:46

10 Answers10

73

I've got similar problem. The reason for that was "Scope" option of "testng" dependency set to "test" when "compile" was needed.

How I fixed it (note, I used Eclipse):

  1. Open pom.xml file.
  2. Go to "Dependencies" tab.
  3. Select "testng" package and click on "Properties..."
  4. On opened screen change "Scope" option to "compile" and click "OK" to save it.
  5. Try to build your project again with "compile test" goals.
MathiasJ
  • 1,661
  • 1
  • 14
  • 20
Pavik
  • 731
  • 5
  • 4
  • Thanks Pavik "compile test" works for me and I am getting error with clean install – Shubham Jain Jan 10 '18 at 07:33
  • For IntelliJ go to File > Project Structure > Modules go to the annotation processing module and change the scope from test to compile. This did it for me too I'm just adding this comment for the intellij users – Nav12 Apr 03 '23 at 16:51
29

remove test scope testng dependency and add compile

<dependency>
        <groupId>org.testng</groupId>
        <artifactId>testng</artifactId>
        <version>6.1.1</version>
        <scope>compile</scope>
    </dependency>
neoerol
  • 911
  • 1
  • 12
  • 18
16

In your pom.xml file you have scope of testng as test

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

Replace the scope by compile

<dependency>
            <groupId>org.testng</groupId>
            <artifactId>testng</artifactId>
            <version>6.9.13.6</version>
            <scope>compile</scope>
</dependency>
Anuj Teotia
  • 1,303
  • 1
  • 15
  • 21
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>
Sanyasi Das
  • 71
  • 1
  • 1
5

Even I had faced this issue and got a solution for the same.

In your pom.xml file remove the scope and this should work fine.

As per below code in pom.xml, remove the scope tag.

Even though we have imported the maven dependency for Testng, when you add scope tag in XML file, it treats as JUnit annotation and not as Testng. So when I removed scope tag, My @Test Annotation was treated as Testng Annotation.

<dependency>
            <groupId>org.testng</groupId>
            <artifactId>testng</artifactId>
            <version>6.8.7</version>
            **<scope>test</scope>** //Remove this line and compile maven
</dependency>
Chethan Shetty
  • 121
  • 2
  • 5
2

test to compile -->

    <dependency>
        <groupId>org.testng</groupId>
        <artifactId>testng</artifactId>
        <version>6.8.8</version>
        <scope>compile</scope>
    </dependency>
Rohit Pol
  • 21
  • 1
1

Make sure your tests are in "src/test/java". It will solve this problem

0

Set testng's scope to "provided" if you're building a web app and don't want it included in WEB-INF/lib.

0

Below solution works for me-

Please add below dependencies in your 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>
Avinash Pande
  • 1,510
  • 19
  • 17
0

For JetBrains IntelliJ users who can't find pom.xml file:

  1. go to 'file' > 'project structure'
  2. select module where test package is located from 'project settings' > 'modules'
  3. go to dependencies tab
  4. change scope setting from 'test' to 'compile'
nuclear_party
  • 91
  • 1
  • 5