Ensure that junit-platform-launcher is on classpath

When testing with Gradle 7.6, junit-platform-launcher won't be
on the test runtime classpath unless it's declared as a dependency.
When testing with Gradle 8.x the dependency is added implicitly but
starting with Gradle 8.3 relying on this will result in a warning.

When junit-platform-launcher is absent, a failure occurs when testing
with Gradle as the class loader structure is such that JUnit tries
to load any test execution listeners, finds the listener declared in
spring-boot-actuator-autoconfigure but cannot then load the
implemented TestExecutionListener interface.

This problem is addressed by augmenting the component metadata for
spring-boot-starter-test to add a dependency on
junit-platform-launcher. This addresses the problem with
spring-boot-actuator-autoconfigure while also addressing a warning
with Gradle 8.3+.

Closes gh-43340
This commit is contained in:
Andy Wilkinson 2025-01-17 16:01:41 +00:00
parent 108119244f
commit 86818b0e45
1 changed files with 11 additions and 1 deletions

View File

@ -1,5 +1,5 @@
/*
* Copyright 2012-2024 the original author or authors.
* Copyright 2012-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -89,6 +89,7 @@ final class JavaPluginAction implements PluginApplicationAction {
project.afterEvaluate(this::configureUtf8Encoding);
configureParametersCompilerArg(project);
configureAdditionalMetadataLocations(project);
configureSpringBootStarterTestToDependOnJUnitPlatformLauncher(project);
}
private void classifyJarTask(Project project) {
@ -317,6 +318,15 @@ final class JavaPluginAction implements PluginApplicationAction {
testImplementation.extendsFrom(testAndDevelopmentOnly);
}
private void configureSpringBootStarterTestToDependOnJUnitPlatformLauncher(Project project) {
project.getDependencies()
.components((components) -> components.withModule("org.springframework.boot:spring-boot-starter-test",
(metadata) -> metadata.withVariant("runtimeElements", (variant) -> variant.withDependencies(
(dependencies) -> dependencies.add("org.junit.platform:junit-platform-launcher")
))));
}
/**
* Task {@link Action} to add additional meta-data locations. We need to use an
* inner-class rather than a lambda due to