How to fix org.testng.TestNGException: Cannot find class [ Updated 2024 ]
How to fix org.testng.TestNGException
Let’s learn how to fix org.testng.TestNGException: Cannot find a class in the classpath in this post. All the test classes are complied with before they are run. This error indicates that the TestNG does not find the .class file to run the test class.
Sample Error Trace
The error has the following stack trace :
org.testng.TestNGException: Cannot find class in classpath: com.testingdocs.testng.tutorials .TestNGAnnotationExecutionOrder at org.testng.xml.XmlClass.loadClass(XmlClass.java:75) at org.testng.xml.XmlClass.init(XmlClass.java:67) at org.testng.xml.XmlClass.(XmlClass.java:54) at org.testng.xml.TestNGContentHandler. startElement(TestNGContentHandler.java:556) at java.xml/com.sun.org.apache.xerces.internal .parsers.AbstractSAXParser.startElement(AbstractSAXParser.java:518) at java.xml/com.sun.org.apache.xerces.internal .impl.dtd.XMLDTDValidator.startElement(XMLDTDValidator.java:731) at java.xml/com.sun.org.apache.xerces.internal .impl.XMLDocumentFragmentScannerImpl .scanStartElement(XMLDocumentFragmentScannerImpl.java:1406) at java.xml/com.sun.org.apache.xerces.internal .impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver .next(XMLDocumentFragmentScannerImpl.java:2725) at java.xml/com.sun.org.apache.xerces.internal .impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:605) at java.xml/com.sun.org.apache.xerces.internal .impl.XMLDocumentFragmentScannerImpl .scanDocument(XMLDocumentFragmentScannerImpl.java:541) at java.xml/com.sun.org.apache.xerces.internal .parsers.XML11Configuration.parse(XML11Configuration.java:888) at java.xml/com.sun.org.apache.xerces.internal .parsers.XML11Configuration.parse(XML11Configuration.java:824) at java.xml/com.sun.org.apache.xerces.internal .parsers.XMLParser.parse(XMLParser.java:141) at java.xml/com.sun.org.apache.xerces.internal .parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1224) at java.xml/com.sun.org.apache.xerces.internal .jaxp.SAXParserImpl$JAXPSAXParser.parse(SAXParserImpl.java:635) at java.xml/com.sun.org.apache.xerces.internal .jaxp.SAXParserImpl.parse(SAXParserImpl.java:324) at java.xml/javax.xml.parsers.SAXParser.parse(SAXParser.java:197) at org.testng.xml.XMLParser.parse(XMLParser.java:39) at org.testng.xml.SuiteXmlParser.parse(SuiteXmlParser.java:16) at org.testng.xml.SuiteXmlParser.parse(SuiteXmlParser.java:9) at org.testng.xml.Parser.parse(Parser.java:162) at org.testng.xml.Parser.parse(Parser.java:248) at org.testng.TestNG.parseSuite(TestNG.java:322) at org.testng.TestNG.initializeSuitesAndJarFile(TestNG.java:374) at org.testng.TestNG.initializeEverything(TestNG.java:1017) at org.testng.remote.support.RemoteTestNG6_12 .initialize(RemoteTestNG6_12.java:22) at org.testng.remote.AbstractRemoteTestNG .run(AbstractRemoteTestNG.java:98) at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:251) at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:77)
Fix
By default, the test classes are generated under the target folder.
<project_home>/target/test-classes
The fix generally depends on the project setup environment, build tool, IDE used etc. If the project is based on Maven using Eclipse IDE, the fix is to clean and update the project.
Check the package names specified in the testng.xml files to the package names in the project. Any mismatch would result in this error. Correct the package names and build the project.
Example
For example, in the above screenshot the fully qualified Test class name in the project is:
com.testingdocs.sample.SkipExceptionExample.java
The error was raised because there was a typo in the class name specified in the testng.xml suite file.
The incorrect class name was:
com.testingdoc.sample.SkipExceptionExample.java
Correct the class name in the testing XML file and clean the project. Run Maven >> Update the project and run the test suite. This error would be resolved.
TestNG Tutorials
TestNG Tutorials on this website can be found at:
For more details on the TestNG Framework, visit the official website of TestNG at: