35

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?

starball
  • 20,030
  • 7
  • 43
  • 238
paragm
  • 355
  • 1
  • 3
  • 6

31 Answers31

47

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.

Marko
  • 20,385
  • 13
  • 48
  • 64
Baer
  • 3,690
  • 25
  • 24
  • 5
    If using Maven, clean project and after update project worked for me. – ovejaexiste Jan 28 '14 at 10:53
  • In Eclipse go to the "Problems" tab and first check that your project is not listed there with a critical error: e.g. "Project cannot be built until error X is fixed." Once those errors are fixed you can do a Project --> Clean – HRVHackers Jun 16 '14 at 06:35
  • whenever i search for a problem with Eclipse i always find a suggestion to clean project and i can see people commenting that it worked for them,but it never worked for me, i had to search for other options. Is there a reason for this.? – Atul Chavan Feb 04 '17 at 11:46
23

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...

MCsuchNsuch
  • 256
  • 2
  • 5
  • 17
Hema
  • 231
  • 2
  • 3
12

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.

mastov
  • 2,942
  • 1
  • 16
  • 33
Shri_Automation
  • 191
  • 1
  • 5
9

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.

goodmayhem
  • 3,364
  • 4
  • 16
  • 17
6

add to pom:

        <build>
            <sourceDirectory>${basedir}/src</sourceDirectory>
            <testSourceDirectory>${basedir}/test</testSourceDirectory>
             (...)
            <plugins>
                (...)
            </plugins>
        </build>
Yevhenii Tsybaiev
  • 1,025
  • 8
  • 7
  • This worked for me, but I don't understand why - could you please explain? – nyarasha Jan 25 '17 at 04:02
  • 1
    @nyarasha This solution just worked for me. If I had to guess, it is because Maven provides TestNG with the directory of where those packages are potentially stored, and then TestNG starts looking recursively. Even just providing ${baseDir} should work as it did for myself. It seems the issue is not providing a directory at all. I am not sure where it looks by default, but providing a valid directory within the project allows TestNG to spider through and find the location of the packages. Maven is then able to build the changes from each package into the target/ folder and run. – JoryAnderson Nov 15 '19 at 00:52
4

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.

Lucky
  • 16,787
  • 19
  • 117
  • 151
priti
  • 639
  • 3
  • 8
  • 25
  • This did work. Thanks a bunch. in ubuntu , i set the classpath as below : /home/user/eclipse-workspace/seleniumGrid/lib/json.jar:/home/user/eclipse-workspace/seleniumGrid/lib/selenium-server-standalone-3.13.0.jar:/home/user/eclipse-workspace/seleniumGrid/lib/testng-6.7.jar:/home/user/eclipse-workspace/seleniumGrid/bin – mrtechmaker Aug 22 '18 at 14:27
  • you can add classpath permanently in your ~/.bashrc file ----------> CLASSPATH=/home/user/eclipse-workspace/seleniumGrid/lib/json.jar:/home/user/eclipse-workspace/seleniumGrid/lib/selenium-server-standalone-3.13.0.jar:/home/user/eclipse-workspace/seleniumGrid/lib/testng-6.7.jar:/home/user/eclipse-workspace/seleniumGrid/bin – mrtechmaker Aug 22 '18 at 14:33
4

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>
Rizier123
  • 58,877
  • 16
  • 101
  • 156
3

in Intellij, what resolved the issue for me was to open the Maven right side toolbar

choose "Clean" , "install" and hit the "Play" button:

see screenshot

Dror
  • 5,107
  • 3
  • 27
  • 45
1

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

1

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

1

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>

Pratik Patel
  • 2,209
  • 3
  • 23
  • 30
1

right click on project -> go to maven -> update project

this fixed my problem when nothing else could

Sachin Singh
  • 383
  • 3
  • 10
1

If none of the above answers work, you can run the test in IDE, get the class path and use it in your command. Ex: If you are using Intellij IDEA, you can find it at the top of the console(screenshot below). Intellij Console

Clicking on the highlighted part expands and displays the complete class path.

you need to remove the references to jars inside the folder: JetBrains\IntelliJ IDEA Community Edition VERSION

java -cp "path_copied" org.testng.TestNG testng.xml

If the project is a Maven project, you can add maven surefire plugin and provide testng suite XML file path, navigate to the project directory and run the command: mvn clean install test

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.12</version>
                <configuration>
                    <suiteXmlFiles>
                        <suiteXmlFile>config/testrun_config.xml</suiteXmlFile>
                    </suiteXmlFiles>
                </configuration>
            </plugin>
Tilak M Divakar
  • 101
  • 1
  • 5
0

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
Ishan Liyanage
  • 2,237
  • 1
  • 26
  • 25
0

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.

Tim Visée
  • 2,988
  • 4
  • 45
  • 55
Mano
  • 1
0

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

0

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.

Jobin Mathew
  • 124
  • 1
  • 8
0

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.
0

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,

nilesh
  • 1
0

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.

Uma Senthil
  • 423
  • 2
  • 5
  • 18
0

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.

sofian
  • 189
  • 1
  • 3
0

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.

Rahul Sharma
  • 2,867
  • 2
  • 27
  • 40
Seploid
  • 61
  • 1
  • 6
0

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.

Qaddaffi
  • 77
  • 1
  • 2
  • 9
0

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

Jens Meinecke
  • 2,904
  • 17
  • 20
Steve
  • 1
0

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.

MD. Khairul Basar
  • 4,976
  • 14
  • 41
  • 59
0

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!

Frits
  • 7,341
  • 10
  • 42
  • 60
Rahul
  • 31
  • 8
0

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.

Unheilig
  • 16,196
  • 193
  • 68
  • 98
0

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 -->
Gaurav Khurana
  • 3,423
  • 2
  • 29
  • 38
0

Was facing a similar issue using IntelliJ and in case it may be helpful to someone else what fixed it for me was to change the Use classpath of module field, to have it as the one that shows the package name.

enter image description here

Francislainy Campos
  • 3,462
  • 4
  • 33
  • 81
0

If you have renamed your testNG class and in the test suite xml file, its not renamed, you will get this error. I corrected in xml file also and it worked for me.

ANIL KUMAR
  • 139
  • 1
  • 3
0

Just problem with the class generating step. You can go to the project folder by explorer and delete classes. After that run "build" your project again. Now, your problem is fixed