Skip to content

Commit 38abddf

Browse files
authored
GH-2750: EmbeddedKafka Set Boot's Bootstrap Prop.
Resolves #2750 Previously, setting the `bootstrapServersProperty` on `@EmbeddedKafka` (or `brokerListProperty` on the broker itself) caused the broker to set that property instead of the default property `spring.embedded.kafka.brokers`. Now, the custom property is set in addition to the default. Also, the default property name is `spring.kafka.bootstrap-servers`. This allows easier testing of Spring Boot applications, without the need for additional configuration.
1 parent 3e1332f commit 38abddf

File tree

7 files changed

+30
-14
lines changed

7 files changed

+30
-14
lines changed

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ Instead of default `spring.embedded.kafka.brokers` system property, the address
133133
For this purpose a `spring.embedded.kafka.brokers.property` (`EmbeddedKafkaBroker.BROKER_LIST_PROPERTY`) system property can be set before starting an embedded Kafka.
134134
For example, with Spring Boot a `spring.kafka.bootstrap-servers` configuration property is expected to be set for auto-configuring Kafka client, respectively.
135135
So, before running tests with an embedded Kafka on random ports, we can set `spring.embedded.kafka.brokers.property=spring.kafka.bootstrap-servers` as a system property - and the `EmbeddedKafkaBroker` will use it to expose its broker addresses.
136+
This is now the default value for this property (starting with version 3.0.10).
136137

137138
With the `EmbeddedKafkaBroker.brokerProperties(Map<String, String>)`, you can provide additional properties for the Kafka servers.
138139
See https://kafka.apache.org/documentation/#brokerconfigs[Kafka Config] for more information about possible broker properties.
@@ -235,7 +236,8 @@ In addition, these properties can be provided:
235236
Essentially these properties mimic some of the `@EmbeddedKafka` attributes.
236237

237238
See more information about configuration properties and how to provide them in the https://junit.org/junit5/docs/current/user-guide/#running-tests-config-params[JUnit 5 User Guide].
238-
For example, a `spring.embedded.kafka.brokers.property=spring.kafka.bootstrap-servers` entry (for testing in Spring Boot application) can be added into a `junit-platform.properties` file in the testing classpath.
239+
For example, a `spring.embedded.kafka.brokers.property=my.bootstrap-servers` entry can be added into a `junit-platform.properties` file in the testing classpath.
240+
Starting with version 3.0.10, the broker automatically sets this to `spring.kafka.bootstrap-servers`, by default, for testing with Spring Boot applications.
239241

240242
NOTE: It is recommended to not combine a global embedded Kafka and per-test class in a single test suite.
241243
Both of them share the same system properties, so it is very likely going to lead to unexpected behavior.
@@ -422,7 +424,7 @@ The following example shows how to use an `@EmbeddedKafka` Annotation to create
422424
----
423425
@RunWith(SpringRunner.class)
424426
@EmbeddedKafka(topics = "someTopic",
425-
bootstrapServersProperty = "spring.kafka.bootstrap-servers")
427+
bootstrapServersProperty = "spring.kafka.bootstrap-servers") // this is now the default
426428
public class MyApplicationTests {
427429
428430
@Autowired
@@ -437,6 +439,7 @@ public class MyApplicationTests {
437439
----
438440
====
439441

442+
NOTE: The `bootstrapServersProperty` is automatically set to `spring.kafka.bootstrap-servers`, by default, starting with version 3.0.10.
440443

441444
==== Hamcrest Matchers
442445

spring-kafka-docs/src/main/asciidoc/whats-new.adoc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,3 +104,5 @@ Similarly, `RECEIVED_MESSAGE_KEY` is replaced by `RECEIVED_KEY` and `RECEIVED_PA
104104

105105
Version 3.0.7 introduced a `MockConsumerFactory` and `MockProducerFactory`.
106106
See <<mock-cons-prod>> for more information.
107+
108+
Starting with version 3.0.10, the embedded Kafka broker, by default, sets the Spring Boot property `spring.kafka.bootstrap-servers` to the address(es) of the embedded broker(s).

spring-kafka-test/src/main/java/org/springframework/kafka/test/EmbeddedKafkaBroker.java

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2018-2022 the original author or authors.
2+
* Copyright 2018-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.
@@ -181,7 +181,7 @@ public class EmbeddedKafkaBroker implements InitializingBean, DisposableBean {
181181

182182
private int zkSessionTimeout = DEFAULT_ZK_SESSION_TIMEOUT;
183183

184-
private String brokerListProperty;
184+
private String brokerListProperty = "spring.kafka.bootstrap-servers";
185185

186186
private volatile ZooKeeperClient zooKeeperClient;
187187

@@ -257,6 +257,8 @@ public EmbeddedKafkaBroker kafkaPorts(int... ports) {
257257

258258
/**
259259
* Set the system property with this name to the list of broker addresses.
260+
* Defaults to {@code spring.kafka.bootstrap-servers} for Spring Boot
261+
* compatibility, since 3.0.10.
260262
* @param brokerListProperty the brokerListProperty to set
261263
* @return this broker.
262264
* @since 2.3
@@ -374,10 +376,10 @@ public void afterPropertiesSet() {
374376
if (this.brokerListProperty == null) {
375377
this.brokerListProperty = System.getProperty(BROKER_LIST_PROPERTY);
376378
}
377-
if (this.brokerListProperty == null) {
378-
this.brokerListProperty = SPRING_EMBEDDED_KAFKA_BROKERS;
379+
if (this.brokerListProperty != null) {
380+
System.setProperty(this.brokerListProperty, getBrokersAsString());
379381
}
380-
System.setProperty(this.brokerListProperty, getBrokersAsString());
382+
System.setProperty(SPRING_EMBEDDED_KAFKA_BROKERS, getBrokersAsString());
381383
System.setProperty(SPRING_EMBEDDED_ZOOKEEPER_CONNECT, getZookeeperConnectionString());
382384
}
383385

@@ -591,7 +593,8 @@ public <T> T doWithAdminFunction(Function<AdminClient, T> callback) {
591593

592594
@Override
593595
public void destroy() {
594-
System.getProperties().remove(brokerListProperty);
596+
System.getProperties().remove(this.brokerListProperty);
597+
System.getProperties().remove(SPRING_EMBEDDED_KAFKA_BROKERS);
595598
System.getProperties().remove(SPRING_EMBEDDED_ZOOKEEPER_CONNECT);
596599
for (KafkaServer kafkaServer : this.kafkaServers) {
597600
try {

spring-kafka-test/src/main/java/org/springframework/kafka/test/context/EmbeddedKafka.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2017-2022 the original author or authors.
2+
* Copyright 2017-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.
@@ -151,13 +151,14 @@
151151
String brokerPropertiesLocation() default "";
152152

153153
/**
154-
* The property name to set with the bootstrap server addresses instead of the default
154+
* The property name to set with the bootstrap server addresses as well as the default
155155
* {@value org.springframework.kafka.test.EmbeddedKafkaBroker#SPRING_EMBEDDED_KAFKA_BROKERS}.
156+
* Defaults to {@code spring.kafka.bootstrap-servers} since 3.0.10.
156157
* @return the property name.
157158
* @since 2.3
158159
* @see org.springframework.kafka.test.EmbeddedKafkaBroker#brokerListProperty(String)
159160
*/
160-
String bootstrapServersProperty() default "";
161+
String bootstrapServersProperty() default "spring.kafka.bootstrap-servers";
161162

162163
/**
163164
* Timeout for internal ZK client connection.

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,8 +119,10 @@ public void testPlanExecutionStarted(TestPlan testPlan) {
119119
this.embeddedKafkaBroker =
120120
new EmbeddedKafkaBroker(count, false, partitions, topics)
121121
.brokerProperties(brokerProperties)
122-
.brokerListProperty(brokerListProperty)
123122
.kafkaPorts(ports);
123+
if (brokerListProperty != null) {
124+
this.embeddedKafkaBroker.brokerListProperty(brokerListProperty);
125+
}
124126
this.embeddedKafkaBroker.afterPropertiesSet();
125127

126128
this.logger.info("Started global Embedded Kafka on: " + this.embeddedKafkaBroker.getBrokersAsString());

spring-kafka-test/src/test/java/org/springframework/kafka/test/EmbeddedKafkaBrokerTests.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2019 the original author or authors.
2+
* Copyright 2019-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.
@@ -30,10 +30,16 @@ public class EmbeddedKafkaBrokerTests {
3030
@Test
3131
void testUpDown() {
3232
EmbeddedKafkaBroker kafka = new EmbeddedKafkaBroker(1);
33+
kafka.brokerListProperty("foo.bar");
3334
kafka.afterPropertiesSet();
3435
assertThat(kafka.getZookeeperConnectionString()).startsWith("127");
36+
assertThat(System.getProperty("foo.bar")).isNotNull();
37+
assertThat(System.getProperty(EmbeddedKafkaBroker.SPRING_EMBEDDED_KAFKA_BROKERS))
38+
.isEqualTo(System.getProperty("foo.bar"));
3539
kafka.destroy();
3640
assertThat(kafka.getZookeeperConnectionString()).isNull();
41+
assertThat(System.getProperty("foo.bar")).isNull();
42+
assertThat(System.getProperty(EmbeddedKafkaBroker.SPRING_EMBEDDED_KAFKA_BROKERS)).isNull();
3743
}
3844

3945
}
Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
11
spring.kafka.embedded.count=2
2-
spring.embedded.kafka.brokers.property=spring.kafka.bootstrap-servers
32
spring.kafka.embedded.topics=topic1,topic2

0 commit comments

Comments
 (0)