TestNG ERROR Cannot find class in classpath

2019-01-14 15:26发布

问题:

I'm trying to run testng via command line and I have following things in classpath:

  • testng jar, jar of compiled test case file, and other required jars.
  • testng.xml points to appropriate class in the hierarchy.

Testng doesn't run instead it throws:

[TestNG] [ERROR]  Cannot find class in classpath: (name of testcase file)

I encountered the same issue before and I just added.

In classpath and it solved the problem. But this time it did not seem to solve it. Any workarounds? Thanks in advance.

回答1:

I just had this problem and I'm pretty confident that it's a bug in the TestNG Plugin for eclipse, since I was able to run my tests from the command line just fine.

The fix that seems to be working for me and others is just to clean your project (ProjectClean). One person had to re-install the whole plugin.



回答2:

If your package name is FabFurnishPackage and your class name is registration then the code in xml should be

<classes>
<class name="FabFurnishPackage.registration">
</classes>

[TestNG] [ERROR] Cannot find class in classpath: registration issue is fixed...



回答3:

Just do Eclipse> Project > Clean and then run the test cases again. It should work.

What it does in background is, that it will call mvn eclipse:clean in your project directory which will delete your .project and .classpath files and you can also do a mvn eclipse:eclipse - this regenerates your .project and .classpath files. Thus adding the desired class in the classpath.



回答4:

I was facing the same issue and what I tried is as below.

  1. Clean the project (Right click on pom.xml and clean)
  2. And update the maven project (Project > Maven > Update Maven Project)

This solved the issue.



回答5:

Before running the command for testng.xml file from command prompt, please check the following if you are missing them

  1. In the command prompt, make sure you are navigating to the folder where you have placed the testng.xml file.
  2. after navigating to that, set CLASSPATH and include the testng jar file location, selenium-server jar file location(if you are working with selenium webdriver), bin folder location of your project which contains all the .class files of your project.
    e.g., set CLASSPATH=C:\Selenium\testng-5.8-jdk15.jar;C:\Selenium\selenium-server-standalone-2.31.0.jar;C:\SeleniumTests\YourProject\bin
  3. Now run the command java org.testng.TestNG testng.xml.

I was into the same situation but the above things worked for me.



回答6:

add to pom:

        <build>
            <sourceDirectory>${basedir}/src</sourceDirectory>
            <testSourceDirectory>${basedir}/test</testSourceDirectory>
             (...)
            <plugins>
                (...)
            </plugins>
        </build>


回答7:

make sure your suite.xml should not have .java extension

ex :

<test name="Test C1">
    <classes>
        <class name="com.taxi.suiteC.TestCase_C1" ></class>  
    </classes>
</test>


回答8:

Update TestNG plugin and It will work.



回答9:

Clean your project (Project → Clean) even if this didn't work just delete the target directory for that module as well as of other module on which your project is dependent.



回答10:

Please make sure that you have specified your class/package in TestNG.xml file.

<test name="test1" preserve-order="false">
    <classes>
        <class name="ABC"/>
    </classes>
</test>



回答11:

I had to make a jar file from my test classes and add it to my class path. Previously I added my test class file to class path and then I got above error. So I created a jar and added to classpath.

e.g

java -cp "my_jars/*" org.testng.TestNG testing.xml


回答12:

Check the class hierarchy. If the package is missing in your class you'll get this error. So, try to create a new testing class by selecting the package.



回答13:

After changing the TestNG Output path(Project Properties-->TestNG-->Output Directory), its worked for me.



回答14:

I just had this problem and was because the skip test option in the run configuration was checked. made that unchecked and build, then run the testNg, worked.



回答15:

I was getting the same error - Cannot find class in classpath:

Solution: I had committed following mistakes:

  1. My class was not under any package. so i created a package and then created my class under that package.
  2. I had not mentioned full path in xml file. ex. . So i also corrected it.
  3. Then i right click on xml file and run as TestNG my program executed successfully.


回答16:

In my case, the package structure was not proper, it was like folders and sub-folders. I deleted that project and imported again with changing the respective jre. After that it worked for me. e.g. Folder structure- TestProject -TestSuite1 Package Structure- TestProject.TestSuite1



回答17:

i was facing same issue solution is , you need to check testng file,your class created under package in this case class is not found through Test NG. perform below steps just convert your project in to Test NG and over write/rplace with new testNG file.

Again run this TestNG in this case it is working my case,



回答18:

If you have your project open in Eclipse(Mars) IDE: Right click on the project in eclipse Click on TestNG Click on "Convert to TestNG"

This creates a new testng.xml file and prompts you to overwrite the existing one (!!!Warning, this will erase your previous testng.xml!!!). Click Yes.

Run as Maven test build.



回答19:

I tried all suggestions above, and it did not work for my case. It turns out that my project build path has an error (one of the dependent jar cannot be found). After fixing that error, testng works again in my Eclipse.



回答20:

To avoid the error

Cannot find class in classpath: (name of testcase file)

you need to hold Runner class and Test class in one place. I mean src and test folders.



回答21:

simple solution was to remove .java extension from class path name. and it worked for me



回答22:

I ran into the same issue. Whenever I ran my test, it kept saying classes not found in the classpath. I tried to fix the testng.xml file, clean up the project and pretty much everything the answers say here, but none worked. Finally I checked my referenced libraries in build path and there were 34 invalid libraries, turns out I had renamed one of the folders from the path that I had taken the jar files from. I fixed the path and the test ran successfully.

So it might be fruitful to check if there are any errors in the referenced jar files before trying the other methods to fix this issue.



回答23:

I have faced the similar issue when I tried to execute my Testng.xml file.

To fix you should move your class file from default pkg to some other pkg; ie create a new pkg and place your class file there



回答24:

I was also facing the same issue. I have tried to set the class path in different way and it works for me:

set classpath="projectpath"\bin;"projectpath"\lib\*     

press Enter

java org.testng.TestNG testng.xml   

press Enter again

I hope this works!



回答25:

I faced same problem but after close investigation.I found i was setting the class path wrong.

here what i was doing.

1-set class path=path for lib*; 2- java org.testng.TestNg testng.xml

Output-Cannot find class in classpath: tests.firstmodule

here firstmodule is my first class.

Solution

1-set class path=path for bin; lib*; 2- java org.testng.TestNg testng.xml



回答26:

When I converted my project with default pacage and a class named addition, to testng.xml. It got converted as:

<classes>
      <class name=".addition"/>
</classes>

and was throwing error that class cannot be loaded. If I remove . before addition, the code works fine. So be careful with default package.



回答27:

I was facing the same issue,

Please check that .java & xml file was created in src folder. Created TestNG class by right clicking on src folder. Works fine!



回答28:

This issue is coming due to some build error. 1. Clean Project - if issue not resolve 2. update maven project - If still not resolved 3. Go to build path from (right click on project- > properties -> java build path) now check library files. if you see an error then resolve it by adding missing file at given location.

I face the same issue, after lots of approaches I found 3rd solution to get that issue fixed



回答29:

Eclipse ->Right Click on Project -> Build Path -> Configure -> Java Build Path.

Add Library -> JRE lib.

Add latest Java lib you are using in the system and remove other old JRE libs and save it.



回答30:

I got the same Problem while using maven

The solution that worked is, the name of the class in testNG file was different then the actual class Name

Since i changed the classname and testng file was not updated.

So make sure className in testNG file should match with the actual className

RestAssuredTest was mistaken written as RestAssuredest in testNG

One of the reason for this problem

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

 <suite name="Suite">
   <test name="Test">
<classes>
  <class name="com.mycompany.app.AppTest"/>
  <class name="com.mycompany.app.RestAssuredTest"/>
 <class name="com.mycompany.app.appiumTests"/>
</classes>
</test> <!-- Test -->
</suite> <!-- Suite -->