Ali Hamadi
Ali Hamadi

Reputation: 683

error: package org.testng.annotations does not exist [javac] import org.testng.annotations.Test;

I have a simple project that I am trying to build using Ant. My project contains testng, but I get an error message when I try to run the build. Could you please help me? I have spent a lot of time trying to see what's the problem but without any results so far.

Here is the project:

package selenium;

import org.testng.annotations.Test;

public class HomePage {

    @Test
    public void test(){

        System.out.println("this is test");
    }
}

Now I have a lib file that contains the testng.jar, and I have a test.xml suite that runs the class, and here is my build file:

    <?xml version="1.0" encoding="UTF-8"?>

<project name="Concep.TestAutomation" basedir="." default="all">

	<property name="src.dir" value="${basedir}/src" />
	<property name="build.dir" value="target" />
	<property name="classes.dir" value="${build.dir}/classes" />	
	<property name="suites.dir" value="suites" />
	<property name="testng.path" value="lib/testng.jar" />
	
	<property name="testreport.dir" value="${build.dir}/test-output" />
	
	<target name="clean" description="Delete the build directory.">
		<delete dir="${build.dir}" />
	</target>

	<target name="init" depends="clean" description="Create build directories.">
		<!--Create the build directory structure used by compile -->
		<mkdir dir="${build.dir}" />
		<mkdir dir="${classes.dir}" />
		<mkdir dir="${testreport.dir}" />
	</target>

	<target name="compile" depends="init" description="Compile tests.">
		<!-- Compile the java code from ${src} into ${classes.dir} -->
		<javac srcdir="${src.dir}" destdir="${classes.dir}" debug="true" />
	</target>

	<target name="run-test" depends="compile" description="Run test suite.">
		<!-- Run a testng suite-->
		<taskdef resource="testngtasks" classpath="${testng.path}" />

		<testng classpathref="${testng.path}" outputDir="${testreport.dir}" haltonfailure="true" verbose="5">
			<xmlfileset dir="${suites.dir}" includes="IA-smoke-test.xml" />
			</testng>
	</target>

	<target name="all" depends="run-test" description="Executes all targets." />
	
		<target name="all-debug" description="outputs all vars in use.">
  <!-- useful to diagnose runtime problems --> 
       <echo message="'work.dir' = '${basedir}/..'"/>
       <echo message="'lib.dir' = '${basedir}/lib'"/>
       <echo message="'src.dir' = '${basedir}/src'"/>
       <echo message="'config.dir' = '${basedir}/config'"/>
       <echo message="'build.dir' = 'target'"/>
       <echo message="'classes.dir' = '${build.dir}/classes'"/>
       <echo message="'suites.dir' = '${basedir}/suites'"/>
       <echo message="'testng.path' = '${basedir}/lib/testng.jar'"/>
     </target>

</project>

Upvotes: 3

Views: 16394

Answers (2)

Chethan Shetty
Chethan Shetty

Reputation: 121

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>

Upvotes: 7

Ali Hamadi
Ali Hamadi

Reputation: 683

so here how I fixed the issue, on the compile target a property were missing which is the "classpathref", from where the project sees all the required libraries to compile it, without the "classpathref" the project doesn't see any external system, so I have done the following to fix it,

<path id="build.classpath">
		<fileset dir="${lib.dir}" includes="**/*.jar" />
		<fileset dir="${src.dir}/concep/selenium/core" includes="**/*.java" />
	</path>

and in my compile target i have added the following property

<javac srcdir="${src.dir}" destdir="${classes.dir}" classpathref="build.classpath" debug="true" includeantruntime="false"/>

Upvotes: 0

Related Questions