Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

ant

testng

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>
like image 908
Ali Hamadi Avatar asked Aug 24 '26 10:08

Ali Hamadi


1 Answers

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>
like image 140
Chethan Shetty Avatar answered Aug 25 '26 23:08

Chethan Shetty



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!