Skip to content

Commit 0bc99b7

Browse files
Addressing PR review
Signed-off-by: chickenchickenlove <[email protected]>
1 parent d825993 commit 0bc99b7

File tree

4 files changed

+57
-13
lines changed

4 files changed

+57
-13
lines changed

spring-kafka-docs/src/main/antora/modules/ROOT/pages/testing.adoc

Lines changed: 50 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
)
124129
public 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 = {
@@ -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")
339343
public class MyApplicationTests {
@@ -350,6 +354,40 @@ 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+
380+
@Bean
381+
public EmbeddedKafkaBroker embeddedKafkaBroker() {
382+
return new EmbeddedKafkaKraftBroker(1, 1, "someTopic");
383+
}
384+
}
385+
386+
}
387+
----
388+
389+
390+
353391
[[kafka-testing-junit4-class-rule]]
354392
=== JUnit4 Class Rule
355393

@@ -378,6 +416,10 @@ public class MyApplicationTests {
378416

379417
Notice that, since this is a Spring Boot application, we override the broker list property to set Spring Boot's property.
380418

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+
381423
[[embedded-broker-with-springjunitconfig-annotations]]
382424
== `@EmbeddedKafka` with `@SpringJunitConfig`
383425

@@ -395,7 +437,7 @@ The following example shows how to use an `@EmbeddedKafka` Annotation to create
395437

396438
[source, java]
397439
----
398-
@RunWith(SpringRunner.class)
440+
@ExtendWith(SpringExtension.class)
399441
@EmbeddedKafka(topics = "someTopic",
400442
bootstrapServersProperty = "spring.kafka.bootstrap-servers") // this is now the default
401443
public class MyApplicationTests {
@@ -404,7 +446,7 @@ public class MyApplicationTests {
404446
private KafkaTemplate<String, String> template;
405447
406448
@Test
407-
public void test() {
449+
void test() {
408450
...
409451
}
410452

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
* <p>
4444
* The typical usage of this annotation is like:
4545
* <pre class="code">
46-
* &#064;RunWith(SpringRunner.class)
46+
* &#064;ExtendWith(SpringExtension.class)
4747
* &#064;EmbeddedKafka
4848
* public class MyKafkaTests {
4949
*
@@ -169,4 +169,3 @@
169169
int adminTimeout() default EmbeddedKafkaBroker.DEFAULT_ADMIN_TIMEOUT;
170170

171171
}
172-

spring-kafka-test/src/main/java/org/springframework/kafka/test/rule/Log4j2LevelAdjuster.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 the original author or authors.
2+
* Copyright 2002-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -37,8 +37,11 @@
3737
* @author Dave Syer
3838
* @author Artem Bilan
3939
* @author Gary Russell
40-
*
40+
* @author Sanghyeok An
41+
* @deprecated since Spring for Apache Kafka 4.0 in favor of the
42+
* {@link org.springframework.kafka.test.condition.LogLevels} and JUnit Jupiter.
4143
*/
44+
@Deprecated(since = "4.0")
4245
public class Log4j2LevelAdjuster implements MethodRule {
4346

4447
private final List<Class<?>> classes;
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17-
package org.springframework.kafka.test.rule;
17+
package org.springframework.kafka.test;
1818

1919
import java.io.IOException;
2020
import java.net.ServerSocket;

0 commit comments

Comments
 (0)