Skip to content

Commit 711d18c

Browse files
authored
GH-2582: GlobalEmbedKafka compatible with native (#2593)
Fixes #2582 Even if we don't use Embedded Kafka in native image or even if Apache Kafka broker (or Zookeeper) is not compatible, the `GlobalEmbeddedKafkaTestExecutionListener` is still service-loaded by JUnit platform and according to build time some classes (`Logger`) are initialized too early * Refactor `GlobalEmbeddedKafkaTestExecutionListener` to initialize `Log` property and check for `TestPlan.getConfigurationParameters()` method lazily already in the `testPlanExecutionStarted()`
1 parent 927c3c7 commit 711d18c

File tree

1 file changed

+13
-20
lines changed

1 file changed

+13
-20
lines changed

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

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2022 the original author or authors.
2+
* Copyright 2022-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -49,8 +49,6 @@
4949
*/
5050
public class GlobalEmbeddedKafkaTestExecutionListener implements TestExecutionListener {
5151

52-
private static final Log LOGGER = LogFactory.getLog(GlobalEmbeddedKafkaTestExecutionListener.class);
53-
5452
/**
5553
* Property name used to enable the {@code GlobalEmbeddedKafkaTestExecutionListener}.
5654
* The {@code GlobalEmbeddedKafkaTestExecutionListener} is registered automatically via
@@ -85,27 +83,22 @@ public class GlobalEmbeddedKafkaTestExecutionListener implements TestExecutionLi
8583
public static final String BROKER_PROPERTIES_LOCATION_PROPERTY_NAME =
8684
"spring.kafka.embedded.broker.properties.location";
8785

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-
10286
private EmbeddedKafkaBroker embeddedKafkaBroker;
10387

88+
private Log logger;
89+
10490
@Override
10591
public void testPlanExecutionStarted(TestPlan testPlan) {
106-
if (!JUNIT_PLATFORM_COMPATIBLE) {
92+
// We have to postpone initialization for native images because of Service Loader at build time.
93+
this.logger = LogFactory.getLog(GlobalEmbeddedKafkaTestExecutionListener.class);
94+
try {
95+
TestPlan.class.getDeclaredMethod("getConfigurationParameters");
96+
}
97+
catch (NoSuchMethodException | SecurityException ex) {
98+
this.logger.debug("JUnit Platform version must be >= 1.8 to use a global embedded kafka server");
10799
return;
108100
}
101+
109102
ConfigurationParameters configurationParameters = testPlan.getConfigurationParameters();
110103
boolean enabled = configurationParameters.getBoolean(LISTENER_ENABLED_PROPERTY_NAME).orElse(false);
111104
if (enabled) {
@@ -130,7 +123,7 @@ public void testPlanExecutionStarted(TestPlan testPlan) {
130123
.kafkaPorts(ports);
131124
this.embeddedKafkaBroker.afterPropertiesSet();
132125

133-
LOGGER.info("Started global Embedded Kafka on: " + this.embeddedKafkaBroker.getBrokersAsString());
126+
this.logger.info("Started global Embedded Kafka on: " + this.embeddedKafkaBroker.getBrokersAsString());
134127
}
135128
}
136129

@@ -156,7 +149,7 @@ private int[] ports(String ports) {
156149
public void testPlanExecutionFinished(TestPlan testPlan) {
157150
if (this.embeddedKafkaBroker != null) {
158151
this.embeddedKafkaBroker.destroy();
159-
LOGGER.info("Stopped global Embedded Kafka.");
152+
this.logger.info("Stopped global Embedded Kafka.");
160153
}
161154
}
162155

0 commit comments

Comments
 (0)