Short description of the problem
I have working script that runs Junit 5 tests programmatically, but when I run the script under Spring Boot project, the script ignores the tests.
The problem in details
I have a program that runs compiled JUnit5 test classes from the code. Here is the code of the program:
public static void main(String[] args) throws Exception {
//creates class loader for loading CalculatorTest (JUnit5 test)
ClassLoader cl = new URLClassLoader(new URL[]{new File("/home/admin/test_folder").toURI().toURL()});
//takes CalculatorTest compiled class from test_folder to start JUnit5 tests
Class cls = cl.loadClass("CalculatorTest");
//start JUnit test and prints results of the test
System.out.println(runTest(cls));
}
public static List<String> runTest(Class clazz) {
Launcher launcher = LauncherFactory.create();
SummaryGeneratingListener listener = new SummaryGeneratingListener();
launcher.registerTestExecutionListeners(listener);
LauncherDiscoveryRequest request = LauncherDiscoveryRequestBuilder.request()
.selectors(selectClass(clazz))
.build();
launcher.execute(request);
//creates report:
List<String> resultList = new ArrayList<>();
TestExecutionSummary summary = listener.getSummary();
System.out.println("test results:");
long totalTestsCount = summary.getTestsFoundCount();
long failedTestsCount = summary.getTestsFailedCount();
long succeededTestsCount = summary.getTestsSucceededCount();
long spentTime = summary.getTimeFinished() - summary.getTimeStarted();
if (failedTestsCount > 0) {
for (TestExecutionSummary.Failure failure : summary.getFailures()) {
StringBuilder sb = new StringBuilder();
sb.append("method ")
.append(failure.getTestIdentifier().getDisplayName())
.append(": ")
.append(failure.getException());
resultList.add(sb.toString());
}
}
return resultList;
}
I made jar files of the program and tested it on a ubuntu server and it worked.
You can see the results here:

After it I copied the script into an endpoint method of a Spring Boot application like this:
@RestController
public class TestRunnerController {
@GetMapping("/run")
public void runTest() throws Exception {
//creates class loader for loading CalculatorTest (JUnit5 test)
ClassLoader cl = new URLClassLoader(new URL[]{new File("/home/admin/test_folder").toURI().toURL()});
//takes CalculatorTest compiled class from test_folder to start JUnit5 tests
Class cls = cl.loadClass("CalculatorTest");
//start JUnit test and prints results of the test
System.out.println(runTest(cls));
}
public static List<String> runTest(Class clazz) {
Launcher launcher = LauncherFactory.create();
SummaryGeneratingListener listener = new SummaryGeneratingListener();
launcher.registerTestExecutionListeners(listener);
LauncherDiscoveryRequest request = LauncherDiscoveryRequestBuilder.request()
.selectors(selectClass(clazz))
.build();
launcher.execute(request);
List<String> resultList = new ArrayList<>();
TestExecutionSummary summary = listener.getSummary();
System.out.println("test results:");
long totalTestsCount = summary.getTestsFoundCount();
long failedTestsCount = summary.getTestsFailedCount();
long succeededTestsCount = summary.getTestsSucceededCount();
long spentTime = summary.getTimeFinished() - summary.getTimeStarted();
if (failedTestsCount > 0) {
for (TestExecutionSummary.Failure failure : summary.getFailures()) {
StringBuilder sb = new StringBuilder();
sb.append("method ")
.append(failure.getTestIdentifier().getDisplayName())
.append(": ")
.append(failure.getException());
resultList.add(sb.toString());
}
}
return resultList;
}
}
I created a jar of the Spring Boot project, run the jar, invoked the endpoint method, it started my script, but script ignores tests! It returns an empty report and even don't print the word HI!
Here is the result of running the test through spring boot jar:

I think the problem is incorrect packing JUnit dependencies into the Jar of the Spring Boot project.
Here is the pom of Spring Boot project:
<dependencies>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>5.9.0</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.9.0</version>
</dependency>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-launcher</artifactId>
<version>1.8.2</version>
</dependency>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-commons</artifactId>
<version>1.8.2</version>
</dependency>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-engine</artifactId>
<version>1.8.2</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<mainClass>app.Application</mainClass>
</configuration>
</plugin>
</plugins>
</build>
Here is the class that I test:
public class Calculator {
public int sum(int a, int b){
System.out.println("HI!");
return a+15+b;
}
}