2

I am getting package org.testng.annotations does not exist error when I am trying to compile multiple source directories using maven. I am running webdriver tests and all of my tests are importing import org.testng.annotations. I am using IntelliJ 12 and my src directory looks like this -

src

  -->main
     --> java
        --> package1
            --> file1.java
        --> package2
            --> file2.java
  -->test
     --> java
        --> package1
           --> file1.java
           --> file2.java
        --> package2
        --> package3
        --> package4
        --> package5
        --> package6

and the build plugin I am using in the pom.xml looks like this -

<?xml version="1.0" encoding="UTF-8"?>
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">

<parent>
    <artifactId>core-xxxxx</artifactId>
    <groupId>core-xxxxx</groupId>
    <version>1.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>tests</artifactId>
   <dependencies>
    <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-api</artifactId>
        <version>2.32.0</version>
    </dependency>
    <dependency>
        <groupId>org.testng</groupId>
        <artifactId>testng</artifactId>
        <version>6.8.1</version>
        <scope>test</scope>
    </dependency>
</dependencies>
    <build>
       <directory>target</directory>
    <outputDirectory>target/classes</outputDirectory>
    <testOutputDirectory>target/test-classes</testOutputDirectory>
 <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>build-helper-maven-plugin</artifactId>
            <executions>
                <execution>
                    <id>add-source</id>
                    <phase>generate-sources</phase>
                    <goals>
                        <goal>add-source</goal>
                    </goals>
                    <configuration>

                        <sources>
                            <source>src/main/java</source>
                            <source>src/test/java</source>
                        </sources>
                    </configuration>
                </execution>
            </executions>
        </plugin>
</plugins>
</build>
</project>

why am I getting the org.testng.annotations not found error?

Dmitry
  • 2,943
  • 1
  • 23
  • 26
Riv P
  • 969
  • 1
  • 11
  • 20
  • 1
    I did take a look here but it does not solve my issue - http://stackoverflow.com/questions/9752972/how-to-add-an-extra-source-directory-for-maven-to-compile-and-include-in-the-bui – Riv P May 16 '13 at 01:49
  • Can you post your full pom file and the full directory structure of your project? – khmarbaise May 16 '13 at 05:22

2 Answers2

1

The default layout for a maven project is the following:

src
   main
      java
   test
      java

within the src/main/java/ directory the package name for you productive java class source files should added. within the src/test/java/ the package name for the unit tests java class source files.

And you should NOT change the layout if you don't have really really good reasons to do so.

Furthermore redefining the defaults of Maven defaults (target, target/classes, target/test-classes etc.) is against the convention over configuration paradigm.

Be aware that you need to follow a naming convention for unit tests which means you unit tests should be named like the following:

<includes>
 <include>**/*Test*.java</include>
 <include>**/*Test.java</include>
 <include>**/*TestCase.java</include>
</includes>

But in your case I assume that we are talking about integration tests which means you need to use the following naming convention

<includes>
 <include>**/IT*.java</include>
 <include>**/*IT.java</include>
 <include>**/*ITCase.java</include>
</includes>

Apart from that you should now that unit tests in Maven will be executed by the maven-surefire-plugin whereas the integration tests will be executed by the maven-failsafe-plugin.

khmarbaise
  • 92,914
  • 28
  • 189
  • 235
  • @khmarbaise-- thanks for your input. the above approach does a clean compile for me (mvn clean compile) but when i run tests with this command mvn clean test -DsuiteFile=resources/test-suites/audioSuite.xml it throws errors that package 2 does not exists which is a package in the first source directory. Any ideas? – Riv P May 16 '13 at 13:44
0

To get around the testng annotations not found errors I ended up moving the base class from src/main/java/package1/baseclass.java to src/main/test/package1/baseclass.java and the testng annotations errors resolved

Riv P
  • 969
  • 1
  • 11
  • 20