@@ -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 = {
@@ -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,40 @@ 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
+
380
+ @Bean
381
+ public EmbeddedKafkaBroker embeddedKafkaBroker() {
382
+ return new EmbeddedKafkaKraftBroker(1, 1, "someTopic");
383
+ }
384
+ }
385
+
386
+ }
387
+ ----
388
+
389
+
390
+
353
391
[[kafka-testing-junit4-class-rule]]
354
392
=== JUnit4 Class Rule
355
393
@@ -378,6 +416,10 @@ public class MyApplicationTests {
378
416
379
417
Notice that, since this is a Spring Boot application, we override the broker list property to set Spring Boot's property.
380
418
419
+ NOTE: The `EmbeddedKafkaRule` JUnit 4 rule has been removed in version 4.0.
420
+ For JUnit 4, you should use the `EmbeddedKafkaKraftBroker` directly or migrate to JUnit 5 with the `@EmbeddedKafka` annotation.
421
+ Please refer to xref:kafka-testing-junit4-embedded-broker[Junit4 Embedded Broker]
422
+
381
423
[[embedded-broker-with-springjunitconfig-annotations]]
382
424
== `@EmbeddedKafka` with `@SpringJunitConfig`
383
425
@@ -395,7 +437,7 @@ The following example shows how to use an `@EmbeddedKafka` Annotation to create
395
437
396
438
[source, java]
397
439
----
398
- @RunWith(SpringRunner .class)
440
+ @ExtendWith(SpringExtension .class)
399
441
@EmbeddedKafka(topics = "someTopic",
400
442
bootstrapServersProperty = "spring.kafka.bootstrap-servers") // this is now the default
401
443
public class MyApplicationTests {
@@ -404,7 +446,7 @@ public class MyApplicationTests {
404
446
private KafkaTemplate<String, String> template;
405
447
406
448
@Test
407
- public void test() {
449
+ void test() {
408
450
...
409
451
}
410
452
0 commit comments