Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Maven Compilation error [package org.testng.annotations does not exist]

Tags:

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 --> 
like image 843
user3060386 Avatar asked Dec 03 '13 07:12

user3060386


People also ask

How to fix Maven test error in Java?

Now click on Run as >Maven Test. This error will definitely get solved. Show activity on this post. Go to: File -> Project Structure -> Project -> Switch Project SDK & Project language level to Java version 1.8 Show activity on this post. My issues was that I was running mvn compile from a child project directory instead of the parent project.

How to add a JAR file to a Maven project?

Simply create a jar from your another project and add it in local lib directory of your current project. Another option is install the jar file in to your local maven repository like below:-

What are the different versions of Maven-compiler-plugin?

I even tried different versions of maven-compiler-plugin: 3.1, 3.7.0, etc. I tried this approach because it seems like the /src/test/java directory is considered a java class that is why it is compiled the same time as /src/test/java.

How to find a missing class in Maven?

Open the jar file of that library in your Maven repo. Check the class is present at the correct location in the jar file. You have posted nothing that could help us finding the problem: the pom is incomplete, the error message is incomplete, we have no idea of the missing class, of the content of your Maven repo, etc.


2 Answers

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.
like image 164
Pavik Avatar answered Sep 17 '22 11:09

Pavik


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> 
like image 29
neoerol Avatar answered Sep 17 '22 11:09

neoerol