@@ -121,15 +121,19 @@ The following example configuration creates topics called `cat` and `hat` with f
121121
122122[source, java]
123123----
124+ @ExtendWith(SpringExtension.class)
125+ @EmbeddedKafka(
126+ partitions = 5,
127+ topics = {"cat", "hat"}
128+ )
124129public class MyTests {
125130
126- @ClassRule
127- private static EmbeddedKafkaRule embeddedKafka = new EmbeddedKafkaRule(1, false, 5, "cat", "hat") ;
131+ @Autowired
132+ private EmbeddedKafkaBroker broker ;
128133
129134 @Test
130135 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));
133137 ...
134138 }
135139
@@ -225,7 +229,7 @@ The following example shows how to use it:
225229
226230[source, java]
227231----
228- @RunWith(SpringRunner .class)
232+ @ExtendWith(SpringExtension .class)
229233@DirtiesContext
230234@EmbeddedKafka(partitions = 1,
231235 topics = {
@@ -237,7 +241,7 @@ public class KafkaStreamsTests {
237241 private EmbeddedKafkaBroker embeddedKafka;
238242
239243 @Test
240- public void someTest() {
244+ void someTest() {
241245 Map<String, Object> consumerProps = KafkaTestUtils.consumerProps("testGroup", "true", this.embeddedKafka);
242246 consumerProps.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "earliest");
243247 ConsumerFactory<Integer, String> cf = new DefaultKafkaConsumerFactory<>(consumerProps);
@@ -333,7 +337,7 @@ The following example shows how to do so:
333337=====
334338[source, java]
335339----
336- @RunWith(SpringRunner .class)
340+ @ExtendWith(SpringExtension .class)
337341@SpringBootTest(properties = "spring.autoconfigure.exclude="
338342 + "org.springframework.cloud.stream.test.binder.TestSupportBinderAutoConfiguration")
339343public class MyApplicationTests {
@@ -350,6 +354,38 @@ They include:
350354* xref:testing.adoc#kafka-testing-junit4-class-rule[JUnit4 Class Rule]
351355* xref:testing.adoc#kafka-testing-embeddedkafka-annotation[`@EmbeddedKafka` Annotation or `EmbeddedKafkaBroker` Bean]
352356
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+
353389[[kafka-testing-junit4-class-rule]]
354390=== JUnit4 Class Rule
355391
@@ -378,6 +414,10 @@ public class MyApplicationTests {
378414
379415Notice that, since this is a Spring Boot application, we override the broker list property to set Spring Boot's property.
380416
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+
381421[[embedded-broker-with-springjunitconfig-annotations]]
382422== `@EmbeddedKafka` with `@SpringJunitConfig`
383423
@@ -395,7 +435,7 @@ The following example shows how to use an `@EmbeddedKafka` Annotation to create
395435
396436[source, java]
397437----
398- @RunWith(SpringRunner .class)
438+ @ExtendWith(SpringExtension .class)
399439@EmbeddedKafka(topics = "someTopic",
400440 bootstrapServersProperty = "spring.kafka.bootstrap-servers") // this is now the default
401441public class MyApplicationTests {
@@ -404,7 +444,7 @@ public class MyApplicationTests {
404444 private KafkaTemplate<String, String> template;
405445
406446 @Test
407- public void test() {
447+ void test() {
408448 ...
409449 }
410450
0 commit comments