Skip to content

Commit c8839a5

Browse files
Addressing PR review
Signed-off-by: chickenchickenlove <ojt90902@naver.com>
1 parent 077adbe commit c8839a5

File tree

4 files changed

+56
-13
lines changed

4 files changed

+56
-13
lines changed

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

Lines changed: 49 additions & 9 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 = {
@@ -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")
339343
public 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

379415
Notice 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
401441
public 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

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

Lines changed: 2 additions & 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
*
@@ -67,6 +67,7 @@
6767
* @author Pawel Lozinski
6868
* @author Adrian Chlebosz
6969
* @author Soby Chacko
70+
* @author Sanghyeok An
7071
*
7172
* @since 1.3
7273
*
@@ -169,4 +170,3 @@
169170
int adminTimeout() default EmbeddedKafkaBroker.DEFAULT_ADMIN_TIMEOUT;
170171

171172
}
172-

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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)