From 86818b0e4583f5231c3197ab498ad5c63082c05d Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Fri, 17 Jan 2025 16:01:41 +0000 Subject: [PATCH] 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 --- .../boot/gradle/plugin/JavaPluginAction.java | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/main/java/org/springframework/boot/gradle/plugin/JavaPluginAction.java b/spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/main/java/org/springframework/boot/gradle/plugin/JavaPluginAction.java index cef1a8e3159..e1981da0218 100644 --- a/spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/main/java/org/springframework/boot/gradle/plugin/JavaPluginAction.java +++ b/spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/main/java/org/springframework/boot/gradle/plugin/JavaPluginAction.java @@ -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