Hi Emond and Tibor, I’m glad you discovered the new LauncherSession API which was added for this purpose. The JUnit 5.8 GA release will come in the next few days.
As you mentrioned, the official documentation does not (yet!) do a good job of explaining its intended use case: https://junit.org/junit5/docs/5.8.0-RC1/user-guide/#launcher-api-launcher-session-listeners-custom Here’s a more complete example that I wrote for Gradle’s test distribution plugin: https://docs.gradle.com/enterprise/test-distribution-gradle-plugin/#junit_5_8_and_later It demonstrates how to initialize a “fixture” only once for an entire session and only if tests are actually going to be executed not just discovered. I’ll make sure to update the official JUnit docs to include a similar example before the release. To only differentiate between the different versions of JUnit in one place and stay backwards compatible, you could create an adapter class like this: public class BackwardsCompatibleLauncherSession implements AutoCloseable { public static BackwardsCompatibleLauncherSession open() { try { LauncherSession launcherSession = LauncherFactory.openSession(); return new BackwardsCompatibleLauncherSession(launcherSession.getLauncher(), launcherSession::close); } catch (NoSuchMethodError ignore) { // JUnit Platform version on test classpath does not yet support launcher sessions return new BackwardsCompatibleLauncherSession(LauncherFactory.create(), () -> {}); } } private final Launcher launcher; private final Runnable onClose; private BackwardsCompatibleLauncherSession(Launcher launcher, Runnable onClose) { this.launcher = launcher; this.onClose = onClose; } Launcher getLauncher() { return launcher; } @Override public void close() { onClose.run(); } } Cheers, Marc
signature.asc
Description: PGP signature