Search code examples
testngtestng-eclipsetestng.xml

Cannot find class in classpath: exception is thrown even with complete hierarchy mentioned for class in testng.xml


I am trying to execute a simple TestNG test method, but, I am getting the exception:

cannot find class in calsspath

I have tried Project->Clean in eclipse, but, it is not working, also, please note that I have given complete hierarchic for the test class in testng.xml, please suggest where the problem might be.

I have tried Project-->Clean, Run as-->mvn clean build

{
       TestNG.xml have below code

        <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
        <suite name="Suite1" verbose="1" >
        <test name="TestClasses" >
        <classes>
        <class name="com.extentreports.test.ExtentReports.TestClass1" />
        </classes>
        </test>
        </suite>
      please see that, i have given complete path for my test class, still issue not solved
        POM.xml have below entries
        <build>
        <plugins>
        <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>3.0.0-M3</version>
        <configuration>
        <suiteXmlFiles> testng.xml </suiteXmlFiles>
        </configuration>
        </plugin>
        </plugins>
        </build>

        <dependencies>
        <!-- https://mvnrepository.com/artifact/com.aventstack/extentreports -->
        <dependency>
        <groupId>com.aventstack</groupId>
        <artifactId>extentreports</artifactId>
        <version>4.0.6</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.testng/testng -->
        <dependency>
        <groupId>org.testng</groupId>
        <artifactId>testng</artifactId>
        <version>6.11</version>
        <scope>test</scope>
        </dependency>
        <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-java</artifactId>
        <version>3.141.59</version>
        </dependency>
       </dependencies>

        TestClass1.java have below code
        @Test
        public void test001(){
        System.out.println("Test001");
        }
}

Error trace in Eclipse Console: { This is the exception trace found in eclipse console{org.testng.TestNGException: Cannot find class in classpath: com.extentreports.test.ExtentReports.TestClass1 at org.testng.xml.XmlClass.loadClass(XmlClass.java:77) at org.testng.xml.XmlClass.init(XmlClass.java:69) at org.testng.xml.XmlClass.(XmlClass.java:55) at org.testng.xml.TestNGContentHandler.startElement(TestNGContentHandler.java:575) at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.startElement(AbstractSAXParser.java:509)}

}


Solution

  • I just found the actual issue, this is happening because Maven will first compile our .java files and creates .class file, and place them in 'target' folder, maven then use these compiled classes for execution. For some unknown reason, the compile is not happening and the target folder is empty. Now, that i have found the issue , I have tried maven's commands like build, generate-source to compile it [not sure if these are the proper commands.but,just gave a try], nothing worked,but, when I run my project [right click on project and select run as ] and run my project as maven test , this have compiled my .java files and generated files in target folder. Now if i run my test class either as individual testng or from testng.xml file, the code got executed successfully.

    This is working for any version of testng like 6.11 ...... 6.14.2 and 6.14.3 too.