20

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
ekad
  • 14,436
  • 26
  • 44
  • 46
Anton
  • 1,344
  • 3
  • 15
  • 32

8 Answers8

16

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.

Justin Tilson
  • 833
  • 1
  • 10
  • 12
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.

Community
  • 1
  • 1
AP22
  • 301
  • 3
  • 7
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

Zeus
  • 6,386
  • 6
  • 54
  • 89
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
Aditi
  • 121
  • 1
  • 1
  • 7
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.

rdj7
  • 1,905
  • 4
  • 18
  • 33
divya
  • 1
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

Rosh_LK
  • 680
  • 1
  • 15
  • 36
0

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
    }
}
NoThisIsPatrick
  • 134
  • 2
  • 11