diff --git a/surefire-providers/surefire-junit-platform/src/main/java/org/apache/maven/surefire/junitplatform/JUnitPlatformProvider.java b/surefire-providers/surefire-junit-platform/src/main/java/org/apache/maven/surefire/junitplatform/JUnitPlatformProvider.java index c8df55be6a..9f075c7918 100644 --- a/surefire-providers/surefire-junit-platform/src/main/java/org/apache/maven/surefire/junitplatform/JUnitPlatformProvider.java +++ b/surefire-providers/surefire-junit-platform/src/main/java/org/apache/maven/surefire/junitplatform/JUnitPlatformProvider.java @@ -25,10 +25,10 @@ import static java.util.Optional.of; import static java.util.logging.Level.WARNING; import static java.util.stream.Collectors.toList; -import static org.apache.maven.surefire.api.booter.ProviderParameterNames.TESTNG_GROUPS_PROP; -import static org.apache.maven.surefire.api.booter.ProviderParameterNames.TESTNG_EXCLUDEDGROUPS_PROP; -import static org.apache.maven.surefire.api.booter.ProviderParameterNames.INCLUDE_JUNIT5_ENGINES_PROP; import static org.apache.maven.surefire.api.booter.ProviderParameterNames.EXCLUDE_JUNIT5_ENGINES_PROP; +import static org.apache.maven.surefire.api.booter.ProviderParameterNames.INCLUDE_JUNIT5_ENGINES_PROP; +import static org.apache.maven.surefire.api.booter.ProviderParameterNames.TESTNG_EXCLUDEDGROUPS_PROP; +import static org.apache.maven.surefire.api.booter.ProviderParameterNames.TESTNG_GROUPS_PROP; import static org.apache.maven.surefire.api.report.ConsoleOutputCapture.startCapture; import static org.apache.maven.surefire.api.util.TestsToRun.fromClass; import static org.apache.maven.surefire.shared.utils.StringUtils.isBlank; @@ -58,6 +58,7 @@ import org.apache.maven.surefire.api.testset.TestListResolver; import org.apache.maven.surefire.api.testset.TestSetFailedException; import org.apache.maven.surefire.api.util.ScanResult; +import org.apache.maven.surefire.api.util.SurefireReflectionException; import org.apache.maven.surefire.api.util.TestsToRun; import org.apache.maven.surefire.shared.utils.StringUtils; import org.junit.platform.engine.DiscoverySelector; @@ -104,7 +105,24 @@ public JUnitPlatformProvider( ProviderParameters parameters ) @Override public Iterable> getSuites() { - return scanClasspath(); + try + { + return scanClasspath(); + } + finally + { + if ( launcher instanceof AutoCloseable ) + { + try + { + ( (AutoCloseable) launcher ).close(); + } + catch ( Exception e ) + { + throw new SurefireReflectionException( e ); + } + } + } } @Override @@ -138,6 +156,17 @@ else if ( forkTestSet == null ) finally { runResult = reporterFactory.close(); + if ( launcher instanceof AutoCloseable ) + { + try + { + ( (AutoCloseable) launcher ).close(); + } + catch ( Exception e ) + { + throw new SurefireReflectionException( e ); + } + } } return runResult; } diff --git a/surefire-providers/surefire-junit-platform/src/main/java/org/apache/maven/surefire/junitplatform/LazyLauncher.java b/surefire-providers/surefire-junit-platform/src/main/java/org/apache/maven/surefire/junitplatform/LazyLauncher.java index 45c9c4913f..caf28b95c3 100644 --- a/surefire-providers/surefire-junit-platform/src/main/java/org/apache/maven/surefire/junitplatform/LazyLauncher.java +++ b/surefire-providers/surefire-junit-platform/src/main/java/org/apache/maven/surefire/junitplatform/LazyLauncher.java @@ -19,6 +19,7 @@ * under the License. */ +import org.apache.maven.surefire.api.util.ReflectionUtils; import org.junit.platform.launcher.Launcher; import org.junit.platform.launcher.LauncherDiscoveryRequest; import org.junit.platform.launcher.TestExecutionListener; @@ -29,8 +30,9 @@ * Launcher proxy which delays the most possible the initialization of the real JUnit * Launcher in order to avoid stream/stdout corruption due to early logging. */ -class LazyLauncher implements Launcher +class LazyLauncher implements Launcher, AutoCloseable { + private AutoCloseable launcherSession; private Launcher launcher; @@ -57,8 +59,26 @@ private Launcher launcher() { if ( launcher == null ) { - launcher = LauncherFactory.create(); + try + { + Class sessionClass = Class.forName( "org.junit.platform.launcher.LauncherSession" ); + launcherSession = ReflectionUtils.invokeGetter( LauncherFactory.class, null, "openSession" ); + launcher = ReflectionUtils.invokeGetter( sessionClass, launcherSession, "getLauncher" ); + } + catch ( ClassNotFoundException e ) + { + launcher = LauncherFactory.create(); + } } return launcher; } + + @Override + public void close() throws Exception + { + if ( launcherSession != null ) + { + launcherSession.close(); + } + } }