I have an external (not in classpath) folder with compiled test class and I want to run JUnit 5 tests from the compiled class programmatically. But JUnit 5 doesn't see the tests method.
The test folder:

The code of Calculator.class:
public class Calculator {
public int sum(int a, int b){
System.out.println("HI!");
return a+15+b;
}
}
Code of CalculatorTest.class:
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
public class CalculatorTest {
Calculator calc = new Calculator();
@Test
void sum() {
System.out.println("test");
assertEquals(5, calc.sum(2,3));
}
}
The code of JUnit5 runner:
@RestController
public class TestRunnerController {
@GetMapping("/run")
public void runTest() throws Exception {
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
runTest(cls);
}
public void 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:");
summary.printTo(new PrintWriter(System.out));
}
}
When I run the code I get:
test results:
Test run finished after 111 ms
[ 1 containers found ]
[ 0 containers skipped ]
[ 1 containers started ]
[ 0 containers aborted ]
[ 1 containers successful ]
[ 0 containers failed ]
[ 0 tests found ]
[ 0 tests skipped ]
[ 0 tests started ]
[ 0 tests aborted ]
[ 0 tests successful ]
[ 0 tests failed ]
I run the program by the followin command: root@box-702307:/home/admin# java -jar myjar.jar
My pom file:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.5.2</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>app</groupId>
<artifactId>rem</artifactId>
<version>1</version>
<properties>
<java.version>11</java.version>
</properties>
<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-engine</artifactId>
<version>5.9.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</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.9.0</version>
</dependency>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-commons</artifactId>
<version>1.9.0</version>
</dependency>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-engine</artifactId>
<version>1.9.0</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>
</project>