Skip to content

Commit 0286e66

Browse files
garyrussellartembilan
authored andcommitted
Set bootstrap servers property in @embeddedkafka
1 parent 2ed8985 commit 0286e66

File tree

8 files changed

+49
-16
lines changed

8 files changed

+49
-16
lines changed

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

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,17 @@ public void setAdminTimeout(int adminTimeout) {
200200
this.adminTimeout = Duration.ofSeconds(adminTimeout);
201201
}
202202

203+
/**
204+
* Set the system property with this name to the list of broker addresses.
205+
* @param brokerListProperty the brokerListProperty to set
206+
* @return this broker.
207+
* @since 2.3
208+
*/
209+
public EmbeddedKafkaBroker brokerListProperty(String brokerListProperty) {
210+
this.brokerListProperty = brokerListProperty;
211+
return this;
212+
}
213+
203214
@Override
204215
public void afterPropertiesSet() {
205216
this.zookeeper = new EmbeddedZookeeper();
@@ -227,7 +238,9 @@ public void afterPropertiesSet() {
227238
}
228239
}
229240
createKafkaTopics(this.topics);
230-
this.brokerListProperty = System.getProperty(BROKER_LIST_PROPERTY);
241+
if (this.brokerListProperty == null) {
242+
this.brokerListProperty = System.getProperty(BROKER_LIST_PROPERTY);
243+
}
231244
if (this.brokerListProperty == null) {
232245
this.brokerListProperty = SPRING_EMBEDDED_KAFKA_BROKERS;
233246
}

spring-kafka-test/src/main/java/org/springframework/kafka/test/condition/EmbeddedKafkaCondition.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,9 @@ private EmbeddedKafkaBroker createBroker(EmbeddedKafka embedded) {
151151
}
152152
}
153153
broker.brokerProperties((Map<String, String>) (Map<?, ?>) properties);
154+
if (StringUtils.hasText(embedded.bootstrapServersProperty())) {
155+
broker.brokerListProperty(embedded.bootstrapServersProperty());
156+
}
154157
broker.afterPropertiesSet();
155158
return broker;
156159
}

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,5 +133,14 @@
133133
*/
134134
String brokerPropertiesLocation() default "";
135135

136+
/**
137+
* The property name to set with the bootstrap server addresses instead of the default
138+
* {@value org.springframework.kafka.test.EmbeddedKafkaBroker#SPRING_EMBEDDED_KAFKA_BROKERS}.
139+
* @return the property name.
140+
* @since 2.3
141+
* @see org.springframework.kafka.test.EmbeddedKafkaBroker#brokerListProperty(String)
142+
*/
143+
String bootstrapServersProperty() default "";
144+
136145
}
137146

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,9 @@ public void customizeContext(ConfigurableApplicationContext context, MergedConte
105105
}
106106

107107
embeddedKafkaBroker.brokerProperties((Map<String, String>) (Map<?, ?>) properties);
108+
if (StringUtils.hasText(this.embeddedKafka.bootstrapServersProperty())) {
109+
embeddedKafkaBroker.brokerListProperty(this.embeddedKafka.bootstrapServersProperty());
110+
}
108111

109112
beanFactory.initializeBean(embeddedKafkaBroker, EmbeddedKafkaBroker.BEAN_NAME);
110113
beanFactory.registerSingleton(EmbeddedKafkaBroker.BEAN_NAME, embeddedKafkaBroker);

spring-kafka-test/src/test/java/org/springframework/kafka/test/condition/EmbeddedKafkaConditionTests.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,20 @@
2222

2323
import org.springframework.kafka.test.EmbeddedKafkaBroker;
2424
import org.springframework.kafka.test.context.EmbeddedKafka;
25+
import org.springframework.kafka.test.utils.KafkaTestUtils;
2526

2627
/**
2728
* @author Gary Russell
2829
* @since 2.3
2930
*
3031
*/
31-
@EmbeddedKafka
32+
@EmbeddedKafka(bootstrapServersProperty = "my.bss.property")
3233
public class EmbeddedKafkaConditionTests {
3334

3435
@Test
3536
public void test(EmbeddedKafkaBroker broker) {
3637
assertThat(broker.getBrokersAsString()).isNotNull();
38+
assertThat(KafkaTestUtils.getPropertyValue(broker, "brokerListProperty")).isEqualTo("my.bss.property");
3739
}
3840

3941
}

spring-kafka-test/src/test/java/org/springframework/kafka/test/context/EmbeddedKafkaContextCustomizerTests.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919
import static org.assertj.core.api.Assertions.assertThat;
2020
import static org.mockito.BDDMockito.given;
21-
import static org.mockito.BDDMockito.mock;
21+
import static org.mockito.Mockito.mock;
2222

2323
import org.junit.Before;
2424
import org.junit.Test;
@@ -30,12 +30,14 @@
3030
import org.springframework.core.annotation.AnnotationUtils;
3131
import org.springframework.core.env.ConfigurableEnvironment;
3232
import org.springframework.kafka.test.EmbeddedKafkaBroker;
33+
import org.springframework.kafka.test.utils.KafkaTestUtils;
3334

3435

3536
/**
3637
* @author Oleg Artyomov
3738
* @author Sergio Lourenco
3839
* @author Artem Bilan
40+
* @author Gary Russell
3941
*
4042
* @since 1.3
4143
*/
@@ -81,6 +83,8 @@ public void testPorts() {
8183

8284
assertThat(factoryStub.getBroker().getBrokersAsString())
8385
.isEqualTo("127.0.0.1:" + annotationWithPorts.ports()[0]);
86+
assertThat(KafkaTestUtils.getPropertyValue(factoryStub.getBroker(), "brokerListProperty"))
87+
.isEqualTo("my.bss.prop");
8488
}
8589

8690

@@ -94,7 +98,7 @@ private class SecondTestWithEmbeddedKafka {
9498

9599
}
96100

97-
@EmbeddedKafka(ports = 8085)
101+
@EmbeddedKafka(ports = 8085, bootstrapServersProperty = "my.bss.prop")
98102
private class TestWithEmbeddedKafkaPorts {
99103

100104
}

src/reference/asciidoc/testing.adoc

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -338,12 +338,8 @@ public class MyApplicationTests {
338338
339339
@ClassRule
340340
public static EmbeddedKafkaRule broker = new EmbeddedKafkaRule(1,
341-
false, "someTopic");
342-
343-
@BeforeClass
344-
public static void setup() {
345-
System.setProperty("spring.kafka.bootstrap-servers",
346-
broker.getEmbeddedKafka().getBrokersAsString());
341+
false, "someTopic")
342+
.brokerListProperty("spring.kafka.bootstrap-servers");
347343
}
348344
349345
@Autowired
@@ -358,6 +354,8 @@ public class MyApplicationTests {
358354
----
359355
====
360356

357+
Notice that, since this is a Spring Boot application, we override the broker list property to set Boot's property.
358+
361359
[[kafka-testing-embeddedkafka-annotation]]
362360
===== `@EmbeddedKafka` Annotation or `EmbeddedKafkaBroker` Bean
363361

@@ -367,14 +365,10 @@ The following example shows how to use an `@EmbeddedKafka` Annotation to create
367365
[source, java]
368366
----
369367
@RunWith(SpringRunner.class)
370-
@EmbeddedKafka(topics = "someTopic")
368+
@EmbeddedKafka(topics = "someTopic",
369+
bootstrapServersProperty = "spring.kafka.bootstrap-servers")
371370
public class MyApplicationTests {
372371
373-
static {
374-
System.setProperty(EmbeddedKafkaBroker.BROKER_LIST_PROPERTY,
375-
"spring.kafka.bootstrap-servers");
376-
}
377-
378372
@Autowired
379373
private KafkaTemplate<String, String> template;
380374

src/reference/asciidoc/whats-new.adoc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,3 +124,8 @@ See <<retrying-deserialization>> for more information.
124124

125125
`ErrorHandlingDeserializer2` now uses a POJO (`FailedDeserializationInfo`) for passing all the contextual information around a deserialization error.
126126
This enables the code to access to extra information that was missing in the old `BiFunction<byte[], Headers, T> failedDeserializationFunction`.
127+
128+
==== EmbeddedKafkaBroker Changes
129+
130+
You can now override the default broker list property name in the annotation.
131+
See <<kafka-testing-embeddedkafka-annotation>> for more information.

0 commit comments

Comments
 (0)