testng-team / testng

TestNG testing framework
https://testng.org
Apache License 2.0
1.99k stars 1.02k forks source link

Class loading disrupted in Servlet context when trying to run test suite packaged in JAR #214

Closed rjmartell closed 5 years ago

rjmartell commented 12 years ago

I'm trying to run a test suite packaged as a JAR archive in a servlet context (i.e. executed in a Tomcat container via JAX-RS endpoint).

Unfortunately the test classes cannot be found, even though the bundled testng.xml file is successfully located (using setTestJar and setXmlPathInJar).

This behavior has been traced to a line in the TestNG.initializeSuitesAndJarFile() method that modifies the thread context ClassLoader:

361 Thread.currentThread().setContextClassLoader(jarLoader);

Commenting out the line (and the two preceding ones) fixes the problem.

NOTE: With this line commented out, a command-line invocation simply requires that the test jar be explicitly added to the classpath, otherwise:

>java -cp testng-6.4a.jar;jcommander-1.23.jar org.testng.TestNG -testjar ets-alpha-1.0-SNAPSHOT.jar 
-xmlpathinjar org/example/ets/alpha/testng.xml

[TestNG] [ERROR]
Cannot find class in classpath: org.example.ets.alpha.StringChecker
rjmartell commented 12 years ago

The issue persists in TestNG 6.5.2.

Running a test suite packaged as a JAR within a web app deployed in Tomcat (v7) produces an exception when trying to instantiate a listener.

Root cause: ''' Caused by: java.lang.NoClassDefFoundError: org/testng/IExecutionListener at java.lang.ClassLoader.defineClass1(Native Method) at java.lang.ClassLoader.defineClass(ClassLoader.java:791) at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142) at java.net.URLClassLoader.defineClass(URLClassLoader.java:449) at java.net.URLClassLoader.access$100(URLClassLoader.java:71) at java.net.URLClassLoader$1.run(URLClassLoader.java:361) at java.net.URLClassLoader$1.run(URLClassLoader.java:355) at java.security.AccessController.doPrivileged(Native Method) at java.net.URLClassLoader.findClass(URLClassLoader.java:354) at java.lang.ClassLoader.loadClass(ClassLoader.java:423) at java.lang.ClassLoader.loadClass(ClassLoader.java:356) at java.lang.Class.forName0(Native Method) at java.lang.Class.forName(Class.java:264) at java.util.ServiceLoader$LazyIterator.next(ServiceLoader.java:362) ... '''

Fixed by commenting out lines 361-363 in TestNG.java (that reset thread context ClassLoader).

krmahadevan commented 5 years ago

Closing this issue. Please comment with a reproducible sample if this is still a problem with 7.0.0