Skip to content

Commit f4b73af

Browse files
artembilangaryrussell
authored andcommitted
Fix NPE in Global Embedded Kafka
The `EmbeddedKafkaBroker.brokerProperties(brokerProperties)` does not accept a `null`. * Fix `brokerProperties` map extraction for a `.orElse(Map.of())` instead of `null` * Modify `GlobalEmbeddedKafkaTestExecutionListenerTests` to ensure that `brokerProperties` are covered both ways - present and missed
1 parent ed6673e commit f4b73af

File tree

2 files changed

+32
-3
lines changed

2 files changed

+32
-3
lines changed

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ public void testPlanExecutionStarted(TestPlan testPlan) {
9898
Integer partitions = configurationParameters.get(PARTITIONS_PROPERTY_NAME, Integer::parseInt).orElse(2);
9999
Map<String, String> brokerProperties =
100100
configurationParameters.get(BROKER_PROPERTIES_LOCATION_PROPERTY_NAME, this::brokerProperties)
101-
.orElse(null);
101+
.orElse(Map.of());
102102
String brokerListProperty = configurationParameters.get(EmbeddedKafkaBroker.BROKER_LIST_PROPERTY)
103103
.orElse(null);
104104
int[] ports =

Diff for: spring-kafka-test/src/test/java/org/springframework/kafka/test/junit/GlobalEmbeddedKafkaTestExecutionListenerTests.java

+31-2
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
import org.apache.kafka.clients.producer.ProducerRecord;
3939
import org.apache.kafka.common.serialization.StringSerializer;
4040
import org.junit.jupiter.api.AfterAll;
41+
import org.junit.jupiter.api.AfterEach;
4142
import org.junit.jupiter.api.BeforeAll;
4243
import org.junit.jupiter.api.Test;
4344
import org.junit.jupiter.api.condition.EnabledIfSystemProperty;
@@ -63,11 +64,39 @@ static void setup() {
6364
@AfterAll
6465
static void tearDown() {
6566
System.clearProperty(GlobalEmbeddedKafkaTestExecutionListener.LISTENER_ENABLED_PROPERTY_NAME);
67+
}
68+
69+
@AfterEach
70+
void cleanUp() {
6671
System.clearProperty(GlobalEmbeddedKafkaTestExecutionListener.BROKER_PROPERTIES_LOCATION_PROPERTY_NAME);
6772
}
6873

6974
@Test
70-
void testGlobalEmbeddedKafkaTestExecutionListener() throws IOException {
75+
void testGlobalEmbeddedKafkaTestExecutionListener() {
76+
var discoveryRequest =
77+
LauncherDiscoveryRequestBuilder.request()
78+
.selectors(DiscoverySelectors.selectClass(TestClass1.class),
79+
DiscoverySelectors.selectClass(TestClass2.class))
80+
.build();
81+
82+
var summaryGeneratingListener = new SummaryGeneratingListener();
83+
LauncherFactory.create().execute(discoveryRequest, summaryGeneratingListener);
84+
85+
var summary = summaryGeneratingListener.getSummary();
86+
87+
try {
88+
assertThat(summary.getTestsStartedCount()).isEqualTo(2);
89+
assertThat(summary.getTestsSucceededCount()).isEqualTo(2);
90+
assertThat(summary.getTestsFailedCount()).isEqualTo(0);
91+
}
92+
catch (Exception ex) {
93+
summary.printFailuresTo(new PrintWriter(System.out));
94+
throw ex;
95+
}
96+
}
97+
98+
@Test
99+
void testGlobalEmbeddedKafkaWithBrokerProperties() throws IOException {
71100
var brokerProperties = new Properties();
72101
brokerProperties.setProperty("auto.create.topics.enable", "false");
73102

@@ -134,7 +163,7 @@ void testCannotAutoCreateTopic() throws ExecutionException, InterruptedException
134163
System.getProperty("spring.kafka.bootstrap-servers"));
135164
producerConfigs.put(ProducerConfig.RETRIES_CONFIG, 1);
136165
producerConfigs.put(ProducerConfig.RETRY_BACKOFF_MS_CONFIG, 1);
137-
producerConfigs.put(ProducerConfig.MAX_BLOCK_MS_CONFIG, 10);
166+
producerConfigs.put(ProducerConfig.MAX_BLOCK_MS_CONFIG, 1000);
138167

139168
StringSerializer serializer = new StringSerializer();
140169
try (var kafkaProducer = new KafkaProducer<>(producerConfigs, serializer, serializer)) {

0 commit comments

Comments
 (0)