简体   繁体   中英

Error: org.testng.TestNGException: Cannot find class in classpath: EmpClass

When i'm trying to run the test suite, am getting this exception. We are using maven project here and i am done with refreshing, cleaning, reinstalling testNG and then imported the maven projects but then also am getting the same exception. Please suggest any ways that am missing here.

Error console:

org.testng.TestNGException: 
Cannot find class in classpath: EmpClass

at org.testng.xml.XmlClass.loadClass(XmlClass.java:81)
at org.testng.xml.XmlClass.init(XmlClass.java:73)
at org.testng.xml.XmlClass.<init>(XmlClass.java:59)
at org.testng.xml.TestNGContentHandler.startElement(TestNGContentHandler.java:543)
at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.startElement(AbstractSAXParser.java:509)
at com.sun.org.apache.xerces.internal.parsers.AbstractXMLDocumentParser.emptyElement(AbstractXMLDocumentParser.java:182)
at com.sun.org.apache.xerces.internal.impl.dtd.XMLDTDValidator.emptyElement(XMLDTDValidator.java:766)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanStartElement(XMLDocumentFragmentScannerImpl.java:1350)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(XMLDocumentFragmentScannerImpl.java:2778)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:606)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:510)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:848)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:777)
at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:141)
at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1213)
at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(SAXParserImpl.java:649)
at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl.parse(SAXParserImpl.java:333)
at javax.xml.parsers.SAXParser.parse(SAXParser.java:195)
at org.testng.xml.XMLParser.parse(XMLParser.java:39)
at org.testng.xml.SuiteXmlParser.parse(SuiteXmlParser.java:17)
at org.testng.xml.SuiteXmlParser.parse(SuiteXmlParser.java:10)
at org.testng.xml.Parser.parse(Parser.java:168)
at org.testng.TestNG.initializeSuitesAndJarFile(TestNG.java:311)
at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:88)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:204)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:175)

I also got the same exception:

I tried the following steps:

  1. In Eclipse> Project > Clean. (The Exception remained)

  2. I ran another project(The testng.xml ran successfully)

After running another project, I ran the project in which I was getting the Exception, and fortunately it worked for me, with no errors.

You have to edit your tests Runner xml file. See this example:

 <test name="Employees Test">
               <classes>
                      <class name="packagename.classname">
                       </class>
               </classes>
 </test>

In <class name="packagename.classname"> row you must enter your tests package name then name of class that you want to run.

Thanks and wish you all the best.

I faced the similar issue on importing maven with testng project. This was solved by converting again into TestNg project by

right click on eclipse project > TestNG > Convert to TestNG

and replacing the existing testng.xml file with the newly created. Clean and update the maven project with mvn install

Note: take backup of your testng.xml

项目>>清洁和 Maven>>更新项目对我有用

Go to Project>Build Path>Configure Build Path>Libraries>Remove Error libraries

After Refresh project and run again program.

My 2 cents on the problem: I was experiencing the same problem, while some part of my path was in Cyrillic. So check the path to your project for special characters. Once I removed this, the problem was fixed

I also ran into the same problem with TestNG version 6.14.2

However, this was due to a foolish mistake on my end and there is no issue whatsoever with maven, testng or eclipse. What I was doing was -

  1. Run project as "mvn clean"
  2. Select "testng.xml" file and right click on it to run as TestNG suite
  3. This was giving me the same error as reported in the original question
  4. I soon realized that I have not compiled the project and the required class files have not been generated that the TestNG would utilize subsequently
  5. The "target" folder will be generated only after the project is compiled (Run as "mvn build")
  6. If your project is compiled, this issue should not be there.

我在使用 selenium 3.11.xx 依赖项时遇到了同样的问题,但是当我使用 selenium 3.4.x 依赖项时我的问题得到了解决。

I had the same error. I cleaned the maven project then the problem was solved.

If you use IntelliJ IDEA, try the following:

  1. From 'File', select "Invalidate Caches/ Restart" option.
  2. Confirm this action by clicking the "Invalidate and Restart" button.
  3. Then the IDEA will be restarted.
  4. You will then find a fresh copy of your project.
  5. You then need to import/ change project format to the Maven project. IDEA will show you options at the bottom right corner.
  6. After this, you should be able to run your project.

I had similar issue and the project had some build errors. I did sudo -R 777 to the project and then I cleaned my project. After that it worked fine.

Hope it helps.

I had the same error when tried to run my tests in a JSF project.

I´m using Eclipse IDE (kepler). So, I did "project > clean" and then ran the tests again of the same project.

It worked!

Note: You have to specify class name along with packages as given below.

<suite name="testNGLearning">
    <test name="simpleTest">
        <classes>
            <class name="testngTests.TestNGSimpleTest" />
            <class name="testngTests.TestMessageUtil" />
        </classes>
    </test>
</suite>

Got the IDE working again. The solution is quite funny - and even a re-install, and deleting the IDE does not fix it on windows. What you need to do in that case, is to go to the testNG run configurations - and delete them all. This is for the TestNG XML classpath error on windows OS. I am using spring tools suite 3.6.4.

Make certain that you delete all the configurations, make sure the project can build, and if there are no problems, the test should run now.

Regards -Anthony Toorie

In testng.xml file, remove "." from tag "" class name if you are not using packages.

include the package name with class. It will remove your name. I also got the same issue but adding the package name with class fixed this issue.

For me the problem was very strange.
I have a testng.xml file at the root of my Eclipse project. When I changed the file through Eclipse, it wasn't changing the testng.xml file in my directory.

Turns out it made a copy of the folder into the eclipse workspace..

Hope this helps someone out

In my opinion if src has test/java/ folders then in build path out location should be like target/test-classes.

If src has main/java/ folders then in build path out location should be like target/classes.

I came across the same issue stated above today. **setting the class-path again in eclipse ** solved the issue .

I had no problems with this until recently.

Now I had to move the suite's xml test file into the root of the project I was testing.

It may be that there is a setting to point at the classes I am testing, but I have not found it.

For me, the issue was because I excluded the test from compilation in the POM. My pom.xml looked like this:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <version>3.5.1</version>
    <configuration>
        <source>1.8</source>
        <target>1.8</target>
        <testExcludes>
            <exclude>com/mycompany/app/AppTest.java</exclude>
        </testExcludes>
    </configuration>
</plugin>

After removing the <exclude> , I was able to run the tests in AppTest.java again.

在我的情况下,我在 eclipse 中将 maven 项目作为默认现有项目导入。之后我作为 maven 项目导入。这对我有用。

This issue can be resolved by adding TestNG.class package to OnlineStore package as below

  1. Step 1: Build Path > Configure Build Path > Java Build Path > Select "Project" tab
  2. Step 2: Click "add" button and select the project with contains the TestNG source.
  3. Step 3 select TestNG.xml run as "TestNG Suite"

I had this same problem when I imported my project from one PC to another on IntelliJ IDEA.

Below Solution worked for me:

  1. Import project using Git.
  2. After Importing IDE would prompt you a dialog on the bottom right corner saying "Import as Gradle", you need to select that but make Sure you have set JAVA_HOME and GRADLE(Gradle Home or Gradle wrapper both should work) properly.
  3. Run the build.gradle and make sure all the dependencies are downloaded correctly without any error.
  4. Now move to your Test Case and try to run with TestNG or JUnit and if you have followed above 3 steps properly you should able to run the test case.

After done trying everything that I found on this issue, in eclipse,

I selected my project --> right click --> Run as --> Maven generate-sources

在此处输入图片说明

Then I re-ran my TestNG project and it ran perfectly fine without any issues.Hope that helps :)

For me it was that I added Skip Tests checkbox/flag in the Maven Build of the Project

Therefore the test classes weren't compiled and then weren't found by TestNG

I also had this issue. I'm using IntelliJ IDEA. I was using src/main/java for configuration files and src/test/java for test files.
Because of this testNG couldn't find the class path. To prevent getting that error need to move all the files from src/main/java to src/test/java or vice versa.

您应该先创建,然后在该包下创建新的 TestNG 类。

确保您的类在 src 下。

Let me share what worked for me:

  1. When we do "mvn clean" - we are cleaning old compiled classes please lgo to ocation "your project directroy -> target " you will find this folder is getting cleaned because that's where MVN places it's compiled artifacts

  2. Since it's already a MAVEN project then go to your project folder and open in command prompt. And issue "mvn clean" and then "mvn test" -> "mvn test" command will place all the compiled files under proper folders and then you can run your tests through TestNG or MAVEN itself.

  3. Make sure you set up "M2_HOME" or "MAVEN_HOME" to your env path , sometime "MAVEN_HOME" doesn't work so add "M2_HOME" as well (google it for this set up)

Above suggestions worked for me so I wanted to share, good luck

It happens when MAVEN is not installed locally on the system.

I faced the same issue when i cloned a Maven based Project from GIT and then as soon as I executed I got exception "Cannot Find Class In ClassPath : Class Name"

So i did a mvn clean test and found that maven is not installed locally.

After doing sudo apt install maven Maven got installed and i was able to Run the code

I was having the same issue when listener was under src/main/java folder and the base class was under src/test/java, I moved base class under src/main/java and it worked perfectly well在此处输入图片说明 l

Same issue here. In my case it was caused by an abstract test class in a separate test package from which all my NG tests were derived. Once I moved that abstract class to the root of the test package, it worked.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM