Skip to content

Commit aec0c07

Browse files
garyrussellartembilan
authored andcommitted
Fix EmbeddedKafkaHolder Example Class Names
Change the JUnit4 rule to `EmbeddedKafkaBroker`. **cherry-pick to 2.7.x**
1 parent 6287d85 commit aec0c07

File tree

1 file changed

+17
-12
lines changed

1 file changed

+17
-12
lines changed

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

+17-12
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ public class MyTests {
151151
By default, `addTopics` will throw an exception when problems arise (such as adding a topic that already exists).
152152
Version 2.6 added a new version of that method that returns a `Map<String, Exception>`; the key is the topic name and the value is `null` for success, or an `Exception` for a failure.
153153

154-
==== Using the Same Brokers for Multiple Test Classes
154+
==== Using the Same Broker(s) for Multiple Test Classes
155155

156156
There is no built-in support for doing so, but you can use the same broker for multiple test classes with something similar to the following:
157157

@@ -160,17 +160,18 @@ There is no built-in support for doing so, but you can use the same broker for m
160160
----
161161
public final class EmbeddedKafkaHolder {
162162
163-
private static EmbeddedKafkaRule embeddedKafka = new EmbeddedKafkaRule(1, false);
163+
private static EmbeddedKafkaBroker embeddedKafka = new EmbeddedKafkaBroker(1, false)
164+
.brokerListProperty("spring.kafka.bootstrap-servers");
164165
165166
private static boolean started;
166167
167-
public static EmbeddedKafkaRule getEmbeddedKafka() {
168+
public static EmbeddedKafkaBroker getEmbeddedKafka() {
168169
if (!started) {
169170
try {
170-
embeddedKafka.before();
171+
embeddedKafka.afterPropertiesSet();
171172
}
172173
catch (Exception e) {
173-
throw new KafkaException(e);
174+
throw new KafkaException("Embedded broker failed to start", e);
174175
}
175176
started = true;
176177
}
@@ -185,20 +186,24 @@ public final class EmbeddedKafkaHolder {
185186
----
186187
====
187188

189+
This assumes a Spring Boot environment and the embedded broker replaces the bootstrap servers property.
190+
188191
Then, in each test class, you can use something similar to the following:
189192

190193
====
191194
[source, java]
192195
----
193196
static {
194-
EmbeddedKafkaHolder.getEmbeddedKafka().addTopics(topic1, topic2);
197+
EmbeddedKafkaHolder.getEmbeddedKafka().addTopics("topic1", "topic2");
195198
}
196199
197-
private static EmbeddedKafkaRule embeddedKafka = EmbeddedKafkaHolder.getEmbeddedKafka();
200+
private static final EmbeddedKafkaBroker broker = EmbeddedKafkaHolder.getEmbeddedKafka();
198201
----
199202
====
200203

201-
IMPORTANT: The preceding example provides no mechanism for shutting down the brokers when all tests are complete.
204+
If you are not using Spring Boot, you can obtain the bootstrap servers using `broker.getBrokersAsString()`.
205+
206+
IMPORTANT: The preceding example provides no mechanism for shutting down the broker(s) when all tests are complete.
202207
This could be a problem if, say, you run your tests in a Gradle daemon.
203208
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.
204209

@@ -293,11 +298,11 @@ When *not* using the spring test context, the `EmbdeddedKafkaCondition` creates
293298
@EmbeddedKafka
294299
public class EmbeddedKafkaConditionTests {
295300
296-
@Test
297-
public void test(EmbeddedKafkaBroker broker) {
298-
String brokerList = broker.getBrokersAsString();
301+
@Test
302+
public void test(EmbeddedKafkaBroker broker) {
303+
String brokerList = broker.getBrokersAsString();
299304
...
300-
}
305+
}
301306
302307
}
303308
----

0 commit comments

Comments
 (0)