21

This error

NoClassDefFoundError: org/testng/TestNG

appears when I'm trying to run my test from Testng.xml file using IntelliJ IDEA. Running my test one by one works perfectly as well as running whole gradle project which points to testng.xml, like grade core-test:test

( On my project I'm using Appium + IntelliJ + TestNG )

But when I'm running testng.xml using IntelliJ I'm immediately getting this message after pressing Run:

Exception in thread "main" java.lang.NoClassDefFoundError: org/testng/TestNG
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:763)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:467)
at java.net.URLClassLoader.access$100(URLClassLoader.java:73)
at java.net.URLClassLoader$1.run(URLClassLoader.java:368)
at java.net.URLClassLoader$1.run(URLClassLoader.java:362)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:361)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at org.testng.RemoteTestNGStarter.main(RemoteTestNGStarter.java:120)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)

Caused by: java.lang.ClassNotFoundException: org.testng.TestNG
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 18 more
1
  • I've got the same error and no solution in sight. Did you figure it out? Commented Dec 20, 2016 at 23:01

9 Answers 9

17

I found a solution posted here: https://intellij-support.jetbrains.com/hc/en-us/community/posts/206597869-Cannot-launch-NGTest-runner-in-IntelliJ-IDEA-2016-1

I got it to work by selecting "In whole project" (I had "In single module" selected) under the Configuration tab in the TestNG "Run/Debug Configurations."

However, the prior configuration worked in IntelliJ IDEA 15, so to me, it seems that it may have come from a breaking change with newer IDE.

It worked for me.

11

adding a screenshot to aid Justin's answer above. The default is In single module which needs to be changed to In whole project.

5

For me it is a little different, i was using testng configuration file to run a testng suite. I was running it from the parent module instead of the child module. The following is what I did to overcome the error

test ng failure

1

Follow the steps - 1. Add the TestNG library from java build path - go to project --> right-click--> properties--> java build path --> third option libraries.

  1. TestNG - sELECT and then apply and close
1

Mine was a little different, using Allure to generate reports with TestNG. In the Allure section of the gradle.build file, I needed to upgrade from 2.8.1 -> 2.13.8. For some reason 2.8.1 was causing the class not found exception for TestNG, some logging improvement needs to be made on their part.

def allureVersion = "2.13.8"

allure {
    autoconfigure = true
    aspectjweaver = true
    version = allureVersion
    clean = true
    useTestNG {
        version = allureVersion
    }
}
1

I had the same problem and found a resolution.

Initially I had a testng.xml file immediately under the project directory. When I right-click the XML to run TestNG, I got "NoClassDefFoundError: org/testng/TestNG".

testng.xml under the subproject's directory

I checked how the RunConfiguration is setup; especially I noticed that the "Use classpath of module". The field was set with value of <rootproject>.<subproject>.

the classpath set for the testng.xml under the subproject directory

I checked "File > Project Structure > Modules" to see how the classpath of the module guru99 is configured. I found the classpath of the module guru99 is almost emtpy; it does not contain the jar of TestNG. This is the reason why the NoClassDefFound error occured.

Countermeasure

I moved the testng.xml file from the subproject's directory into the src/test/resources directory. I mean, I got <root project>/<subproject>/src/test/resoures/testng.xml.

XML in the src/test/resource directory

Then I tried right-click the testng.xml in the src/test/resources directory. It ran successfull!

What's the difference? I checked the Run Configuration for the XML, especially I checked the "classpath" set by IntelliJ. IntelliJ successfully chose the classpath of the Module <root project><subproject>.test for the XML.

Renewed classpath to run the testng.xml

The classpath was full of external jars including TestNG, which were configured by Gradle build.gradle as testImplementation dependencies.

So, as far as I observed, IntelliJ changes its behavior by the location where testng.xml is saved. IntelliJ seems expecting the XML file to be located under the <subproject>/src/test/resource directory. If so, you can right-click the XML and run it quickly. If not (you located the XML outside the <subproject>/src/test/resources), IntelliJ fails to configure the appropriate classpath. In that case, you will see NoClassDefFoundError for org/testng/TestNG.

My opinion

The documentation of TestNG tells us that we can locate the XML wherever we like. Gradle allows us to locate the XML wherever inside the project. In a build.gradle we can write:

test {
    useTestNG {
        suites "the path where we save the xml"
    }
}

However, IntelliJ's TestNG plugin seems to be opinionated where you should save the the testng.xml. It requires us to locate the testng.xml in the <subproject>/src/test/resources directory. Otherwise, IntelliJ's TestNG plugin fails to buid appropriate classpath to carry out processing for TestNG.

0

PLease follow following steps:

  1. Add the TestNG library from in java build path for the project.

  2. Right click on the project then -> Build path -> Configure build path -> Libraries tab -> Add library -> Choose TestNG and apply.

  3. Create a testNG class and run the testng.xml file, it will work.

  4. Right click on project -> testNG-> create testNG class -> give the details and create one method in that class then run the file.

0

for me, this was a little different still. I just started using IntelliJ in 2019, and it worked fine until around Christmas. Here is what finally fixed my misbehaving system that couldn't run IntelliJ (because i could run the same files from the same repository on another system running the same IntelliJ version)

Had to change the template TestNG to use a Test Kind of Suite instead of Class, then set this to whole project. showing where i changed the project to Whole Project

0

Tried all the answers mentioned in this post, in addition to all this I had to remove Build from the section Before launch: Activate tool window to make it work:

Screen Shot

Not the answer you're looking for? Browse other questions tagged or ask your own question.