TestNG ERROR Cannot find class in classpath

2019-01-14 15:37发布

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.

30条回答
看我几分像从前
2楼-- · 2019-01-14 16:05

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.

查看更多
Explosion°爆炸
3楼-- · 2019-01-14 16:06

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

查看更多
▲ chillily
4楼-- · 2019-01-14 16:07

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!

查看更多
Luminary・发光体
5楼-- · 2019-01-14 16:08

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.

查看更多
Ridiculous、
6楼-- · 2019-01-14 16:10

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.

查看更多
地球回转人心会变
7楼-- · 2019-01-14 16:11

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 -->
查看更多
登录 后发表回答