Skip to content

Commit 70035e7

Browse files
authored
GH-2481: Global Embedded Broker and JUnit Platform
Resolves #2481 The Maven Surefire plugin uses an older version of JUnit platform which is incompatible with the Global embedded broker. Check that the platform version is compatible before attempting to determine if a global embedded broker should be created. Tested with a Spring Boot application. * Add doc comment. * Add author.
1 parent 0723add commit 70035e7

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

spring-kafka-docs/src/main/asciidoc/testing.adoc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,7 @@ This could be a problem if, say, you run your tests in a Gradle daemon.
221221
You should not use this technique in such a situation, or you should use something to call `destroy()` on the `EmbeddedKafkaBroker` when your tests are complete.
222222

223223
Starting with version 3.0, the framework exposes a `GlobalEmbeddedKafkaTestExecutionListener` for the JUnit Platform; it is disabled by default.
224+
This requires JUnit Platform 1.8 or greater.
224225
The purpose of this listener is to start one global `EmbeddedKafkaBroker` for the whole test plan and stop it at the end of the plan.
225226
To enable this listener, and therefore have a single global embedded Kafka cluster for all the tests in the project, the `spring.kafka.global.embedded.enabled` property must be set to `true` via system properties or JUnit Platform configuration.
226227
In addition, these properties can be provided:

spring-kafka-test/src/main/java/org/springframework/kafka/test/junit/GlobalEmbeddedKafkaTestExecutionListener.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
* to enable it.
4444
*
4545
* @author Artem Bilan
46+
* @author Gary Russell
4647
*
4748
* @since 3.0
4849
*/
@@ -84,10 +85,27 @@ public class GlobalEmbeddedKafkaTestExecutionListener implements TestExecutionLi
8485
public static final String BROKER_PROPERTIES_LOCATION_PROPERTY_NAME =
8586
"spring.kafka.embedded.broker.properties.location";
8687

88+
private static final boolean JUNIT_PLATFORM_COMPATIBLE;
89+
90+
static {
91+
boolean compat = false;
92+
try {
93+
TestPlan.class.getDeclaredMethod("getConfigurationParameters");
94+
compat = true;
95+
}
96+
catch (NoSuchMethodException | SecurityException e) {
97+
LOGGER.debug("JUnit Platform version must be >= 1.8 to use a global embedded kafka server");
98+
}
99+
JUNIT_PLATFORM_COMPATIBLE = compat;
100+
}
101+
87102
private EmbeddedKafkaBroker embeddedKafkaBroker;
88103

89104
@Override
90105
public void testPlanExecutionStarted(TestPlan testPlan) {
106+
if (!JUNIT_PLATFORM_COMPATIBLE) {
107+
return;
108+
}
91109
ConfigurationParameters configurationParameters = testPlan.getConfigurationParameters();
92110
boolean enabled = configurationParameters.getBoolean(LISTENER_ENABLED_PROPERTY_NAME).orElse(false);
93111
if (enabled) {

0 commit comments

Comments
 (0)