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条回答
Explosion°爆炸
2楼-- · 2019-01-14 16:22

add to pom:

        <build>
            <sourceDirectory>${basedir}/src</sourceDirectory>
            <testSourceDirectory>${basedir}/test</testSourceDirectory>
             (...)
            <plugins>
                (...)
            </plugins>
        </build>
查看更多
爱情/是我丢掉的垃圾
3楼-- · 2019-01-14 16:24

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.
查看更多
够拽才男人
4楼-- · 2019-01-14 16:25

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,

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

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

查看更多
啃猪蹄的小仙女
6楼-- · 2019-01-14 16:27

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.

查看更多
做个烂人
7楼-- · 2019-01-14 16:28

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.

查看更多
登录 后发表回答