@@ -121,15 +121,19 @@ The following example configuration creates topics called `cat` and `hat` with f
121
121
122
122
[source, java]
123
123
----
124
+ @ExtendWith(SpringExtension.class)
125
+ @EmbeddedKafka(
126
+ partitions = 5,
127
+ topics = {"cat", "hat"}
128
+ )
124
129
public class MyTests {
125
130
126
- @ClassRule
127
- private static EmbeddedKafkaRule embeddedKafka = new EmbeddedKafkaRule(1, false, 5, "cat", "hat") ;
131
+ @Autowired
132
+ private EmbeddedKafkaBroker broker ;
128
133
129
134
@Test
130
135
public void test() {
131
- embeddedKafkaRule.getEmbeddedKafka()
132
- .addTopics(new NewTopic("thing1", 10, (short) 1), new NewTopic("thing2", 15, (short) 1));
136
+ broker.addTopics(new NewTopic("thing1", 10, (short) 1), new NewTopic("thing2", 15, (short) 1));
133
137
...
134
138
}
135
139
@@ -225,7 +229,7 @@ The following example shows how to use it:
225
229
226
230
[source, java]
227
231
----
228
- @RunWith(SpringRunner .class)
232
+ @ExtendWith(SpringExtension .class)
229
233
@DirtiesContext
230
234
@EmbeddedKafka(partitions = 1,
231
235
topics = {
@@ -237,7 +241,7 @@ public class KafkaStreamsTests {
237
241
private EmbeddedKafkaBroker embeddedKafka;
238
242
239
243
@Test
240
- public void someTest() {
244
+ void someTest() {
241
245
Map<String, Object> consumerProps = KafkaTestUtils.consumerProps("testGroup", "true", this.embeddedKafka);
242
246
consumerProps.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "earliest");
243
247
ConsumerFactory<Integer, String> cf = new DefaultKafkaConsumerFactory<>(consumerProps);
@@ -333,7 +337,7 @@ The following example shows how to do so:
333
337
=====
334
338
[source, java]
335
339
----
336
- @RunWith(SpringRunner .class)
340
+ @ExtendWith(SpringExtension .class)
337
341
@SpringBootTest(properties = "spring.autoconfigure.exclude="
338
342
+ "org.springframework.cloud.stream.test.binder.TestSupportBinderAutoConfiguration")
339
343
public class MyApplicationTests {
@@ -350,6 +354,38 @@ They include:
350
354
* xref:testing.adoc#kafka-testing-junit4-class-rule[JUnit4 Class Rule]
351
355
* xref:testing.adoc#kafka-testing-embeddedkafka-annotation[`@EmbeddedKafka` Annotation or `EmbeddedKafkaBroker` Bean]
352
356
357
+ [[kafka-testing-junit4-embedded-broker]]
358
+ === Junit4 Embedded Broker
359
+
360
+ The following example shows how to create an embedded broker in Junit4:
361
+ [source, java]
362
+ ----
363
+ @SpringBootTest
364
+ public class MyApplicationTests {
365
+
366
+ @Autowired
367
+ private final EmbeddedKafkaBroker broker;
368
+
369
+ @Autowired
370
+ private KafkaTemplate<String, String> template;
371
+
372
+ @Test
373
+ public void test() {
374
+ ...
375
+ }
376
+
377
+ @Configuration
378
+ public static class MyConfiguration {
379
+ @Bean
380
+ public EmbeddedKafkaBroker embeddedKafkaBroker() {
381
+ return new EmbeddedKafkaKraftBroker(1, 1, "someTopic");
382
+ }
383
+
384
+ }
385
+
386
+ }
387
+ ----
388
+
353
389
[[kafka-testing-junit4-class-rule]]
354
390
=== JUnit4 Class Rule
355
391
@@ -378,6 +414,10 @@ public class MyApplicationTests {
378
414
379
415
Notice that, since this is a Spring Boot application, we override the broker list property to set Spring Boot's property.
380
416
417
+ NOTE: The `EmbeddedKafkaRule` JUnit 4 rule has been removed in version 4.0.
418
+ For JUnit 4, you should use the `EmbeddedKafkaKraftBroker` directly or migrate to JUnit 5 with the `@EmbeddedKafka` annotation.
419
+ Please refer to xref:kafka-testing-junit4-embedded-broker[Junit4 Embedded Broker]
420
+
381
421
[[embedded-broker-with-springjunitconfig-annotations]]
382
422
== `@EmbeddedKafka` with `@SpringJunitConfig`
383
423
@@ -395,7 +435,7 @@ The following example shows how to use an `@EmbeddedKafka` Annotation to create
395
435
396
436
[source, java]
397
437
----
398
- @RunWith(SpringRunner .class)
438
+ @ExtendWith(SpringExtension .class)
399
439
@EmbeddedKafka(topics = "someTopic",
400
440
bootstrapServersProperty = "spring.kafka.bootstrap-servers") // this is now the default
401
441
public class MyApplicationTests {
@@ -404,7 +444,7 @@ public class MyApplicationTests {
404
444
private KafkaTemplate<String, String> template;
405
445
406
446
@Test
407
- public void test() {
447
+ void test() {
408
448
...
409
449
}
410
450
0 commit comments