New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Programmatic Loading of Tests in External Jar #1999
Comments
@revof11 - Is this issue related to this stack overflow post ? Also can you please try again using TestNG If the problem still exists, request you to please provide a complete sample that can be used to reproduce the issue. The code you shared does not show how the jars were created and the contents of the jar as well. |
I'll refactor it to see what I can do with beta2. Looks like I'll need to refactor a bit. When I pulled the dependency from Maven central it yelled at me about the org.testng.TestNG class not being available but it doesn't complain with beta1. I'll do beta1 and then refactor into beta2. Looks like in beta 1 I just need to fix:
|
@revof11 - |
ping @revof11 - Any updates on this ? |
I have retried this using TestNG I built a standalone jar which contains just 1 test class [ its a mavenised project wherein the test class resided under Here's how the test class looks like (this is what resides in the jar) package com.rationaleemotions;
import org.testng.Reporter;
import org.testng.annotations.Test;
public class AppTest {
@Test
public void testMethod() {
Reporter.log("Hello world", true);
}
} Here's the method private static void runTests(File jar) throws MalformedURLException {
TestNG testNg = new TestNG();
URLClassLoader customClassLoader = URLClassLoader.newInstance(new URL[]{jar.toURI().toURL()});
testNg.addClassLoader(customClassLoader);
XmlSuite suite = new XmlSuite();
suite.setName("Invoked Run");
XmlTest test = new XmlTest();
test.setXmlSuite(suite);
test.setName("Invoked Run");
test.setParallel(XmlSuite.ParallelMode.NONE);
XmlClass xmlClass = new XmlClass("com.rationaleemotions.AppTest");
test.setClasses(Collections.singletonList(xmlClass));
suite.setTests(Collections.singletonList(test));
System.err.println(suite.toXml());
testNg.setXmlSuites(Collections.singletonList(suite));
testNg.setVerbose(2);
testNg.run();
int status = testNg.getStatus();
System.err.println("Status ===> " + status);
} Output
|
@krmahadevan Have you tried this by executing one jar into another using command line arguments ? |
@akshaybadgujarjs - What version are you using ? |
I'm using 7.0.0 My use case is I wanted to create one executor service which can take up any jar and be able to execute any test present in 2nd jar. I have developed simple application and trying to execute the same. Please find github repo for demo and test repos https://github.com/akshaybadgujarjs/demo Please let me know in case any more information required. |
The latest released version of TestNG is |
Sure will try and get back to you.! |
Faced same issue with updated test ng version as well. Log text file attached for your reference. |
@akshaybadgujarjs - Sounds like you are facing #2770 |
My concern is not with java.nio.file.FileAlreadyExistsException: test-output/testng-reports.css this exception. |
ah ok! Yeah. My bad. I got sidelined with that exception. I will take a look at this and let you know what is going on. |
No problem. Thanks for your assistance.! |
@akshaybadgujarjs - I can't reproduce the problem. The code works fine for me. I cloned both your repositories and then built your test project. After that I applied this patch patchdiff --git a/build.gradle b/build.gradle
index 2905641..221d411 100644
--- a/build.gradle
+++ b/build.gradle
@@ -2,6 +2,7 @@ plugins {
id 'java'
id 'org.springframework.boot' version '2.4.13'
id 'io.spring.dependency-management' version '1.0.15.RELEASE'
+ id 'idea'
}
group = 'com.example'
@@ -21,8 +22,7 @@ dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
// https://mvnrepository.com/artifact/org.testng/testng
-// implementation group: 'org.testng', name: 'testng', version: '7.5'
- implementation group: 'org.testng', name: 'testng', version: '7.0.0'
+ implementation group: 'org.testng', name: 'testng', version: '7.6.1'
compileOnly "org.projectlombok:lombok:${lombokVersion}"
annotationProcessor "org.projectlombok:lombok:${lombokVersion}"
@@ -36,5 +36,5 @@ tasks.named('test') {
}
bootJar {
- mainClass = 'com.example.demo.DemoApplication'
-}
\ No newline at end of file
+ mainClass.set('com.example.demo.DemoApplication')
+}
diff --git a/src/main/java/com/example/demo/SampleController.java b/src/main/java/com/example/demo/SampleController.java
index b3d753b..7cd2788 100644
--- a/src/main/java/com/example/demo/SampleController.java
+++ b/src/main/java/com/example/demo/SampleController.java
@@ -1,5 +1,6 @@
package com.example.demo;
+import java.net.MalformedURLException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@@ -7,11 +8,11 @@ import org.springframework.web.bind.annotation.RestController;
@RestController
public class SampleController {
- @Autowired
- private TestRunner testRunner;
+ @Autowired
+ private SimpleTestRunner simpleTestRunner;
- @GetMapping("/execute-tests")
- void executeTests(){
- testRunner.triggerTestFromRequest();
- }
+ @GetMapping("/execute-tests")
+ public void executeTests() throws MalformedURLException {
+ simpleTestRunner.runTestCase();
+ }
}
diff --git a/src/main/java/com/example/demo/SimpleTestRunner.java b/src/main/java/com/example/demo/SimpleTestRunner.java
new file mode 100644
index 0000000..6938de0
--- /dev/null
+++ b/src/main/java/com/example/demo/SimpleTestRunner.java
@@ -0,0 +1,74 @@
+package com.example.demo;
+
+import java.io.File;
+import java.net.MalformedURLException;
+import java.net.URL;
+import java.net.URLClassLoader;
+import java.util.Collections;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.beans.factory.annotation.Configurable;
+import org.springframework.stereotype.Component;
+import org.springframework.stereotype.Service;
+import org.testng.IInvokedMethod;
+import org.testng.IInvokedMethodListener;
+import org.testng.ITestResult;
+import org.testng.TestNG;
+import org.testng.xml.XmlClass;
+import org.testng.xml.XmlSuite;
+import org.testng.xml.XmlTest;
+
+@Slf4j
+@Service
+@Component
+@Configurable
+public class SimpleTestRunner {
+
+ private static final String JAR =
+ "/Users/krishnamahadevan/temp/testng-1999/test/build/libs/test-0.0.1-SNAPSHOT.jar";
+
+ public void runTestCase() throws MalformedURLException {
+ TestNG testNg = new TestNG();
+
+ URLClassLoader customClassLoader = URLClassLoader.newInstance(new URL[]{getURL()});
+ testNg.addClassLoader(customClassLoader);
+ XmlSuite suite = new XmlSuite();
+ suite.setName("Pulsar Tests Suite");
+
+ XmlTest test = new XmlTest();
+ test.setXmlSuite(suite);
+ test.setName("Pulsar Test");
+ test.setParallel(XmlSuite.ParallelMode.NONE);
+ XmlClass xmlClass = new XmlClass("com.example.test.PulsarTestsSample");
+ test.setClasses(Collections.singletonList(xmlClass));
+ test.setVerbose(2);
+ suite.setTests(Collections.singletonList(test));
+ log.info(suite.toXml());
+ testNg.setXmlSuites(Collections.singletonList(suite));
+
+ testNg.addListener(new LocalListener());
+ testNg.setVerbose(2);
+ testNg.run();
+ int status = testNg.getStatus();
+ log.info("Status ===> {} ", status);
+ }
+
+
+ private static URL getURL() {
+ try {
+ URL url = new File(JAR).toURI().toURL();
+ log.info("Working with URL {}", url);
+ return url;
+ } catch (MalformedURLException e) {
+ log.error("Could not load URL. Root cause: {}", e.getMessage(), e);
+ throw new RuntimeException(e);
+ }
+ }
+
+ public static class LocalListener implements IInvokedMethodListener {
+
+ @Override
+ public void beforeInvocation(IInvokedMethod method, ITestResult testResult) {
+ log.info("About to run {}", method.getTestMethod().getQualifiedName());
+ }
+ }
+} Now when I ran your main application and then triggered a get on I have attached the logs. |
I will give it a try and confirm. |
Hello @krmahadevan I checked it. It is working fine in IDE. I suppose you have executed it from IDE as well. Is that correct ? Because from IDE even I didn't faced any issue previously. FYI, I'm using given command to create build and run it ==> To Run |
I didnt see you mention anywhere on the mode of running it. So I ran it from within the IDE. I was able to get the CLI running by using java -cp build/libs/demo-0.0.1-SNAPSHOT.jar -Dloader.path=file:/Users/krishnamahadevan/temp/testng-1999/test/build/libs/ org.springframework.boot.loader.PropertiesLauncher Refer here |
Actually it was my first question about running it via command line. |
@akshaybadgujarjs - It works for me This is the command that I am using:
|
Okay thank you @krmahadevan for all the assistance and help here🙂 |
It worked as expected. Thank you so much.! 🙂 |
TestNG Version
6.14.3
Expected behavior
TestNG runs tests using added ClassLoader.
Actual behavior
TestNG not finding tests in ClassLoader.
Is the issue reproductible on runner?
Test case sample
When I run debug and hit the
customClassLoader
, I can run this successfully:It retrieves the annotations appropriately as well.
The text was updated successfully, but these errors were encountered: