From 2da4d5d3ebb6243a74d6f976dba88ecd951fad1e Mon Sep 17 00:00:00 2001 From: chickenchickenlove Date: Wed, 30 Apr 2025 00:13:03 +0900 Subject: [PATCH 1/7] spring-projectsGH-3873: Deprecate JUnit 4 utilities in the project Signed-off-by: chickenchickenlove --- build.gradle | 4 -- .../Log4j2LevelAdjuster.java | 52 ++++++++++--------- .../kafka/test/extensions/package-info.java | 5 ++ .../kafka/test/rule/package-info.java | 5 -- .../AddressableEmbeddedBrokerTests.java | 2 +- 5 files changed, 33 insertions(+), 35 deletions(-) rename spring-kafka-test/src/main/java/org/springframework/kafka/test/{rule => extensions}/Log4j2LevelAdjuster.java (58%) create mode 100644 spring-kafka-test/src/main/java/org/springframework/kafka/test/extensions/package-info.java delete mode 100644 spring-kafka-test/src/main/java/org/springframework/kafka/test/rule/package-info.java rename spring-kafka-test/src/test/java/org/springframework/kafka/test/{rule => extensions}/AddressableEmbeddedBrokerTests.java (98%) diff --git a/build.gradle b/build.gradle index 037066766f..f9f9765b84 100644 --- a/build.gradle +++ b/build.gradle @@ -57,7 +57,6 @@ ext { hibernateValidationVersion = '8.0.2.Final' jacksonBomVersion = '2.18.3' jaywayJsonPathVersion = '2.9.0' - junit4Version = '4.13.2' junitJupiterVersion = '5.12.2' kafkaVersion = '4.0.0' kotlinCoroutinesVersion = '1.10.2' @@ -378,9 +377,6 @@ project ('spring-kafka-test') { api 'org.junit.platform:junit-platform-launcher' optionalApi "org.hamcrest:hamcrest-core:$hamcrestVersion" optionalApi "org.mockito:mockito-core:$mockitoVersion" - optionalApi ("junit:junit:$junit4Version") { - exclude group: 'org.hamcrest', module: 'hamcrest-core' - } optionalApi "org.apache.logging.log4j:log4j-core:$log4jVersion" } } diff --git a/spring-kafka-test/src/main/java/org/springframework/kafka/test/rule/Log4j2LevelAdjuster.java b/spring-kafka-test/src/main/java/org/springframework/kafka/test/extensions/Log4j2LevelAdjuster.java similarity index 58% rename from spring-kafka-test/src/main/java/org/springframework/kafka/test/rule/Log4j2LevelAdjuster.java rename to spring-kafka-test/src/main/java/org/springframework/kafka/test/extensions/Log4j2LevelAdjuster.java index 31fd8bd9fa..649eaa0ef1 100755 --- a/spring-kafka-test/src/main/java/org/springframework/kafka/test/rule/Log4j2LevelAdjuster.java +++ b/spring-kafka-test/src/main/java/org/springframework/kafka/test/extensions/Log4j2LevelAdjuster.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2019 the original author or authors. + * Copyright 2002-2025 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -14,32 +14,34 @@ * limitations under the License. */ -package org.springframework.kafka.test.rule; +package org.springframework.kafka.test.extensions; +import java.lang.reflect.Method; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.List; import org.apache.logging.log4j.Level; -import org.junit.rules.MethodRule; -import org.junit.runners.model.FrameworkMethod; -import org.junit.runners.model.Statement; +import org.junit.jupiter.api.extension.ExtensionContext; +import org.junit.jupiter.api.extension.InvocationInterceptor; +import org.junit.jupiter.api.extension.ReflectiveInvocationContext; import org.springframework.kafka.test.utils.JUnitUtils; import org.springframework.kafka.test.utils.JUnitUtils.LevelsContainer; /** - * A JUnit method @Rule that changes the logger level for a set of classes + * A JUnit extension @ that changes the logger level for a set of classes * while a test method is running. Useful for performance or scalability tests * where we don't want to generate a large log in a tight inner loop. * * @author Dave Syer * @author Artem Bilan * @author Gary Russell + * @author Sanghyoek An * */ -public class Log4j2LevelAdjuster implements MethodRule { +public class Log4j2LevelAdjuster implements InvocationInterceptor { private final List> classes; @@ -60,25 +62,25 @@ public Log4j2LevelAdjuster categories(String... categoriesToAdjust) { } @Override - public Statement apply(final Statement base, FrameworkMethod method, Object target) { - return new Statement() { - @Override - public void evaluate() throws Throwable { - LevelsContainer container = null; - try { - container = JUnitUtils.adjustLogLevels(method.getName(), - Log4j2LevelAdjuster.this.classes, Log4j2LevelAdjuster.this.categories, - Log4j2LevelAdjuster.this.level); - base.evaluate(); - } - finally { - if (container != null) { - JUnitUtils.revertLevels(method.getName(), container); - } - } - } + public void interceptTestMethod(Invocation invocation, + ReflectiveInvocationContext invocationContext, + ExtensionContext extensionContext) throws Throwable { + String methodName = extensionContext.getRequiredTestMethod().getName(); + LevelsContainer container = null; - }; + try { + container = JUnitUtils.adjustLogLevels( + methodName, + Log4j2LevelAdjuster.this.classes, + Log4j2LevelAdjuster.this.categories, + Log4j2LevelAdjuster.this.level); + invocation.proceed(); + } + finally { + if (container != null) { + JUnitUtils.revertLevels(methodName, container); + } + } } } diff --git a/spring-kafka-test/src/main/java/org/springframework/kafka/test/extensions/package-info.java b/spring-kafka-test/src/main/java/org/springframework/kafka/test/extensions/package-info.java new file mode 100644 index 0000000000..0268df36b5 --- /dev/null +++ b/spring-kafka-test/src/main/java/org/springframework/kafka/test/extensions/package-info.java @@ -0,0 +1,5 @@ +/** + * Provides JUnit5 extensions. + */ +@org.jspecify.annotations.NullMarked +package org.springframework.kafka.test.extensions; diff --git a/spring-kafka-test/src/main/java/org/springframework/kafka/test/rule/package-info.java b/spring-kafka-test/src/main/java/org/springframework/kafka/test/rule/package-info.java deleted file mode 100644 index 44664bc27b..0000000000 --- a/spring-kafka-test/src/main/java/org/springframework/kafka/test/rule/package-info.java +++ /dev/null @@ -1,5 +0,0 @@ -/** - * Provides JUnit rules. - */ -@org.jspecify.annotations.NullMarked -package org.springframework.kafka.test.rule; diff --git a/spring-kafka-test/src/test/java/org/springframework/kafka/test/rule/AddressableEmbeddedBrokerTests.java b/spring-kafka-test/src/test/java/org/springframework/kafka/test/extensions/AddressableEmbeddedBrokerTests.java similarity index 98% rename from spring-kafka-test/src/test/java/org/springframework/kafka/test/rule/AddressableEmbeddedBrokerTests.java rename to spring-kafka-test/src/test/java/org/springframework/kafka/test/extensions/AddressableEmbeddedBrokerTests.java index 3b73ef8194..2b6ba92b2a 100644 --- a/spring-kafka-test/src/test/java/org/springframework/kafka/test/rule/AddressableEmbeddedBrokerTests.java +++ b/spring-kafka-test/src/test/java/org/springframework/kafka/test/extensions/AddressableEmbeddedBrokerTests.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package org.springframework.kafka.test.rule; +package org.springframework.kafka.test.extensions; import java.io.IOException; import java.net.ServerSocket; From 077adbee50b81777a09bcefc0ae6464906049cb3 Mon Sep 17 00:00:00 2001 From: chickenchickenlove Date: Thu, 1 May 2025 12:37:48 +0900 Subject: [PATCH 2/7] Revert "spring-projectsGH-3873: Deprecate JUnit 4 utilities in the project" This reverts commit 2da4d5d3ebb6243a74d6f976dba88ecd951fad1e. Signed-off-by: chickenchickenlove --- build.gradle | 4 ++ .../kafka/test/extensions/package-info.java | 5 -- .../Log4j2LevelAdjuster.java | 52 +++++++++---------- .../kafka/test/rule/package-info.java | 5 ++ .../AddressableEmbeddedBrokerTests.java | 2 +- 5 files changed, 35 insertions(+), 33 deletions(-) delete mode 100644 spring-kafka-test/src/main/java/org/springframework/kafka/test/extensions/package-info.java rename spring-kafka-test/src/main/java/org/springframework/kafka/test/{extensions => rule}/Log4j2LevelAdjuster.java (58%) create mode 100644 spring-kafka-test/src/main/java/org/springframework/kafka/test/rule/package-info.java rename spring-kafka-test/src/test/java/org/springframework/kafka/test/{extensions => rule}/AddressableEmbeddedBrokerTests.java (98%) diff --git a/build.gradle b/build.gradle index f9f9765b84..037066766f 100644 --- a/build.gradle +++ b/build.gradle @@ -57,6 +57,7 @@ ext { hibernateValidationVersion = '8.0.2.Final' jacksonBomVersion = '2.18.3' jaywayJsonPathVersion = '2.9.0' + junit4Version = '4.13.2' junitJupiterVersion = '5.12.2' kafkaVersion = '4.0.0' kotlinCoroutinesVersion = '1.10.2' @@ -377,6 +378,9 @@ project ('spring-kafka-test') { api 'org.junit.platform:junit-platform-launcher' optionalApi "org.hamcrest:hamcrest-core:$hamcrestVersion" optionalApi "org.mockito:mockito-core:$mockitoVersion" + optionalApi ("junit:junit:$junit4Version") { + exclude group: 'org.hamcrest', module: 'hamcrest-core' + } optionalApi "org.apache.logging.log4j:log4j-core:$log4jVersion" } } diff --git a/spring-kafka-test/src/main/java/org/springframework/kafka/test/extensions/package-info.java b/spring-kafka-test/src/main/java/org/springframework/kafka/test/extensions/package-info.java deleted file mode 100644 index 0268df36b5..0000000000 --- a/spring-kafka-test/src/main/java/org/springframework/kafka/test/extensions/package-info.java +++ /dev/null @@ -1,5 +0,0 @@ -/** - * Provides JUnit5 extensions. - */ -@org.jspecify.annotations.NullMarked -package org.springframework.kafka.test.extensions; diff --git a/spring-kafka-test/src/main/java/org/springframework/kafka/test/extensions/Log4j2LevelAdjuster.java b/spring-kafka-test/src/main/java/org/springframework/kafka/test/rule/Log4j2LevelAdjuster.java similarity index 58% rename from spring-kafka-test/src/main/java/org/springframework/kafka/test/extensions/Log4j2LevelAdjuster.java rename to spring-kafka-test/src/main/java/org/springframework/kafka/test/rule/Log4j2LevelAdjuster.java index 649eaa0ef1..31fd8bd9fa 100755 --- a/spring-kafka-test/src/main/java/org/springframework/kafka/test/extensions/Log4j2LevelAdjuster.java +++ b/spring-kafka-test/src/main/java/org/springframework/kafka/test/rule/Log4j2LevelAdjuster.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2025 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -14,34 +14,32 @@ * limitations under the License. */ -package org.springframework.kafka.test.extensions; +package org.springframework.kafka.test.rule; -import java.lang.reflect.Method; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.List; import org.apache.logging.log4j.Level; -import org.junit.jupiter.api.extension.ExtensionContext; -import org.junit.jupiter.api.extension.InvocationInterceptor; -import org.junit.jupiter.api.extension.ReflectiveInvocationContext; +import org.junit.rules.MethodRule; +import org.junit.runners.model.FrameworkMethod; +import org.junit.runners.model.Statement; import org.springframework.kafka.test.utils.JUnitUtils; import org.springframework.kafka.test.utils.JUnitUtils.LevelsContainer; /** - * A JUnit extension @ that changes the logger level for a set of classes + * A JUnit method @Rule that changes the logger level for a set of classes * while a test method is running. Useful for performance or scalability tests * where we don't want to generate a large log in a tight inner loop. * * @author Dave Syer * @author Artem Bilan * @author Gary Russell - * @author Sanghyoek An * */ -public class Log4j2LevelAdjuster implements InvocationInterceptor { +public class Log4j2LevelAdjuster implements MethodRule { private final List> classes; @@ -62,25 +60,25 @@ public Log4j2LevelAdjuster categories(String... categoriesToAdjust) { } @Override - public void interceptTestMethod(Invocation invocation, - ReflectiveInvocationContext invocationContext, - ExtensionContext extensionContext) throws Throwable { - String methodName = extensionContext.getRequiredTestMethod().getName(); - LevelsContainer container = null; - - try { - container = JUnitUtils.adjustLogLevels( - methodName, - Log4j2LevelAdjuster.this.classes, - Log4j2LevelAdjuster.this.categories, - Log4j2LevelAdjuster.this.level); - invocation.proceed(); - } - finally { - if (container != null) { - JUnitUtils.revertLevels(methodName, container); + public Statement apply(final Statement base, FrameworkMethod method, Object target) { + return new Statement() { + @Override + public void evaluate() throws Throwable { + LevelsContainer container = null; + try { + container = JUnitUtils.adjustLogLevels(method.getName(), + Log4j2LevelAdjuster.this.classes, Log4j2LevelAdjuster.this.categories, + Log4j2LevelAdjuster.this.level); + base.evaluate(); + } + finally { + if (container != null) { + JUnitUtils.revertLevels(method.getName(), container); + } + } } - } + + }; } } diff --git a/spring-kafka-test/src/main/java/org/springframework/kafka/test/rule/package-info.java b/spring-kafka-test/src/main/java/org/springframework/kafka/test/rule/package-info.java new file mode 100644 index 0000000000..44664bc27b --- /dev/null +++ b/spring-kafka-test/src/main/java/org/springframework/kafka/test/rule/package-info.java @@ -0,0 +1,5 @@ +/** + * Provides JUnit rules. + */ +@org.jspecify.annotations.NullMarked +package org.springframework.kafka.test.rule; diff --git a/spring-kafka-test/src/test/java/org/springframework/kafka/test/extensions/AddressableEmbeddedBrokerTests.java b/spring-kafka-test/src/test/java/org/springframework/kafka/test/rule/AddressableEmbeddedBrokerTests.java similarity index 98% rename from spring-kafka-test/src/test/java/org/springframework/kafka/test/extensions/AddressableEmbeddedBrokerTests.java rename to spring-kafka-test/src/test/java/org/springframework/kafka/test/rule/AddressableEmbeddedBrokerTests.java index 2b6ba92b2a..3b73ef8194 100644 --- a/spring-kafka-test/src/test/java/org/springframework/kafka/test/extensions/AddressableEmbeddedBrokerTests.java +++ b/spring-kafka-test/src/test/java/org/springframework/kafka/test/rule/AddressableEmbeddedBrokerTests.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package org.springframework.kafka.test.extensions; +package org.springframework.kafka.test.rule; import java.io.IOException; import java.net.ServerSocket; From c8839a565a0f074684649ba894b6ecab6024596f Mon Sep 17 00:00:00 2001 From: chickenchickenlove Date: Thu, 1 May 2025 14:08:01 +0900 Subject: [PATCH 3/7] Addressing PR review Signed-off-by: chickenchickenlove --- .../antora/modules/ROOT/pages/testing.adoc | 58 ++++++++++++++++--- .../kafka/test/context/EmbeddedKafka.java | 4 +- .../kafka/test/rule/Log4j2LevelAdjuster.java | 5 +- .../AddressableEmbeddedBrokerTests.java | 2 +- 4 files changed, 56 insertions(+), 13 deletions(-) rename spring-kafka-test/src/test/java/org/springframework/kafka/test/{rule => }/AddressableEmbeddedBrokerTests.java (98%) diff --git a/spring-kafka-docs/src/main/antora/modules/ROOT/pages/testing.adoc b/spring-kafka-docs/src/main/antora/modules/ROOT/pages/testing.adoc index b4d6438855..ee5274fab0 100644 --- a/spring-kafka-docs/src/main/antora/modules/ROOT/pages/testing.adoc +++ b/spring-kafka-docs/src/main/antora/modules/ROOT/pages/testing.adoc @@ -121,15 +121,19 @@ The following example configuration creates topics called `cat` and `hat` with f [source, java] ---- +@ExtendWith(SpringExtension.class) +@EmbeddedKafka( + partitions = 5, + topics = {"cat", "hat"} +) public class MyTests { - @ClassRule - private static EmbeddedKafkaRule embeddedKafka = new EmbeddedKafkaRule(1, false, 5, "cat", "hat"); + @Autowired + private EmbeddedKafkaBroker broker; @Test public void test() { - embeddedKafkaRule.getEmbeddedKafka() - .addTopics(new NewTopic("thing1", 10, (short) 1), new NewTopic("thing2", 15, (short) 1)); + broker.addTopics(new NewTopic("thing1", 10, (short) 1), new NewTopic("thing2", 15, (short) 1)); ... } @@ -225,7 +229,7 @@ The following example shows how to use it: [source, java] ---- -@RunWith(SpringRunner.class) +@ExtendWith(SpringExtension.class) @DirtiesContext @EmbeddedKafka(partitions = 1, topics = { @@ -237,7 +241,7 @@ public class KafkaStreamsTests { private EmbeddedKafkaBroker embeddedKafka; @Test - public void someTest() { + void someTest() { Map consumerProps = KafkaTestUtils.consumerProps("testGroup", "true", this.embeddedKafka); consumerProps.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "earliest"); ConsumerFactory cf = new DefaultKafkaConsumerFactory<>(consumerProps); @@ -333,7 +337,7 @@ The following example shows how to do so: ===== [source, java] ---- -@RunWith(SpringRunner.class) +@ExtendWith(SpringExtension.class) @SpringBootTest(properties = "spring.autoconfigure.exclude=" + "org.springframework.cloud.stream.test.binder.TestSupportBinderAutoConfiguration") public class MyApplicationTests { @@ -350,6 +354,38 @@ They include: * xref:testing.adoc#kafka-testing-junit4-class-rule[JUnit4 Class Rule] * xref:testing.adoc#kafka-testing-embeddedkafka-annotation[`@EmbeddedKafka` Annotation or `EmbeddedKafkaBroker` Bean] +[[kafka-testing-junit4-embedded-broker]] +=== Junit4 Embedded Broker + +The following example shows how to create an embedded broker in Junit4: +[source, java] +---- +@SpringBootTest +public class MyApplicationTests { + + @Autowired + private final EmbeddedKafkaBroker broker; + + @Autowired + private KafkaTemplate template; + + @Test + public void test() { + ... + } + + @Configuration + public static class MyConfiguration { + @Bean + public EmbeddedKafkaBroker embeddedKafkaBroker() { + return new EmbeddedKafkaKraftBroker(1, 1, "someTopic"); + } + + } + +} +---- + [[kafka-testing-junit4-class-rule]] === JUnit4 Class Rule @@ -378,6 +414,10 @@ public class MyApplicationTests { Notice that, since this is a Spring Boot application, we override the broker list property to set Spring Boot's property. +NOTE: The `EmbeddedKafkaRule` JUnit 4 rule has been removed in version 4.0. +For JUnit 4, you should use the `EmbeddedKafkaKraftBroker` directly or migrate to JUnit 5 with the `@EmbeddedKafka` annotation. +Please refer to xref:kafka-testing-junit4-embedded-broker[Junit4 Embedded Broker] + [[embedded-broker-with-springjunitconfig-annotations]] == `@EmbeddedKafka` with `@SpringJunitConfig` @@ -395,7 +435,7 @@ The following example shows how to use an `@EmbeddedKafka` Annotation to create [source, java] ---- -@RunWith(SpringRunner.class) +@ExtendWith(SpringExtension.class) @EmbeddedKafka(topics = "someTopic", bootstrapServersProperty = "spring.kafka.bootstrap-servers") // this is now the default public class MyApplicationTests { @@ -404,7 +444,7 @@ public class MyApplicationTests { private KafkaTemplate template; @Test - public void test() { + void test() { ... } diff --git a/spring-kafka-test/src/main/java/org/springframework/kafka/test/context/EmbeddedKafka.java b/spring-kafka-test/src/main/java/org/springframework/kafka/test/context/EmbeddedKafka.java index 1e35a71e1e..e6d6a0c499 100644 --- a/spring-kafka-test/src/main/java/org/springframework/kafka/test/context/EmbeddedKafka.java +++ b/spring-kafka-test/src/main/java/org/springframework/kafka/test/context/EmbeddedKafka.java @@ -43,7 +43,7 @@ *

* The typical usage of this annotation is like: *

- * @RunWith(SpringRunner.class)
+ * @ExtendWith(SpringExtension.class)
  * @EmbeddedKafka
  * public class MyKafkaTests {
  *
@@ -67,6 +67,7 @@
  * @author Pawel Lozinski
  * @author Adrian Chlebosz
  * @author Soby Chacko
+ * @author Sanghyeok An
  *
  * @since 1.3
  *
@@ -169,4 +170,3 @@
 	int adminTimeout() default EmbeddedKafkaBroker.DEFAULT_ADMIN_TIMEOUT;
 
 }
-
diff --git a/spring-kafka-test/src/main/java/org/springframework/kafka/test/rule/Log4j2LevelAdjuster.java b/spring-kafka-test/src/main/java/org/springframework/kafka/test/rule/Log4j2LevelAdjuster.java
index 31fd8bd9fa..1a90dca7f2 100755
--- a/spring-kafka-test/src/main/java/org/springframework/kafka/test/rule/Log4j2LevelAdjuster.java
+++ b/spring-kafka-test/src/main/java/org/springframework/kafka/test/rule/Log4j2LevelAdjuster.java
@@ -37,8 +37,11 @@
  * @author Dave Syer
  * @author Artem Bilan
  * @author Gary Russell
- *
+ * @author Sanghyeok An
+ * @deprecated since Spring for Apache Kafka 4.0 in favor of the
+ * {@link org.springframework.kafka.test.condition.LogLevels} and JUnit Jupiter.
  */
+@Deprecated(since = "4.0")
 public class Log4j2LevelAdjuster implements MethodRule {
 
 	private final List> classes;
diff --git a/spring-kafka-test/src/test/java/org/springframework/kafka/test/rule/AddressableEmbeddedBrokerTests.java b/spring-kafka-test/src/test/java/org/springframework/kafka/test/AddressableEmbeddedBrokerTests.java
similarity index 98%
rename from spring-kafka-test/src/test/java/org/springframework/kafka/test/rule/AddressableEmbeddedBrokerTests.java
rename to spring-kafka-test/src/test/java/org/springframework/kafka/test/AddressableEmbeddedBrokerTests.java
index 3b73ef8194..ceccb9476e 100644
--- a/spring-kafka-test/src/test/java/org/springframework/kafka/test/rule/AddressableEmbeddedBrokerTests.java
+++ b/spring-kafka-test/src/test/java/org/springframework/kafka/test/AddressableEmbeddedBrokerTests.java
@@ -14,7 +14,7 @@
  * limitations under the License.
  */
 
-package org.springframework.kafka.test.rule;
+package org.springframework.kafka.test;
 
 import java.io.IOException;
 import java.net.ServerSocket;

From 6fe547088569bc622acc6e5603d13844b77f24c5 Mon Sep 17 00:00:00 2001
From: chickenchickenlove 
Date: Thu, 1 May 2025 14:15:28 +0900
Subject: [PATCH 4/7] Fixes lint error.

Signed-off-by: chickenchickenlove 
---
 .../kafka/test/AddressableEmbeddedBrokerTests.java               | 1 -
 1 file changed, 1 deletion(-)

diff --git a/spring-kafka-test/src/test/java/org/springframework/kafka/test/AddressableEmbeddedBrokerTests.java b/spring-kafka-test/src/test/java/org/springframework/kafka/test/AddressableEmbeddedBrokerTests.java
index ceccb9476e..8c58e651df 100644
--- a/spring-kafka-test/src/test/java/org/springframework/kafka/test/AddressableEmbeddedBrokerTests.java
+++ b/spring-kafka-test/src/test/java/org/springframework/kafka/test/AddressableEmbeddedBrokerTests.java
@@ -32,7 +32,6 @@
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.context.annotation.Bean;
 import org.springframework.context.annotation.Configuration;
-import org.springframework.kafka.test.EmbeddedKafkaKraftBroker;
 import org.springframework.kafka.test.utils.KafkaTestUtils;
 import org.springframework.test.annotation.DirtiesContext;
 import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;

From 53a76861e6516d70303566726a047b357100ccde Mon Sep 17 00:00:00 2001
From: chickenchickenlove 
Date: Fri, 2 May 2025 18:39:18 +0900
Subject: [PATCH 5/7] Addressing PR review

Signed-off-by: chickenchickenlove 
---
 .../antora/modules/ROOT/pages/testing.adoc    | 78 ++-----------------
 .../kafka/test/context/EmbeddedKafka.java     |  2 +-
 .../kafka/test/rule/Log4j2LevelAdjuster.java  |  8 +-
 3 files changed, 11 insertions(+), 77 deletions(-)

diff --git a/spring-kafka-docs/src/main/antora/modules/ROOT/pages/testing.adoc b/spring-kafka-docs/src/main/antora/modules/ROOT/pages/testing.adoc
index ee5274fab0..9c8c37cc45 100644
--- a/spring-kafka-docs/src/main/antora/modules/ROOT/pages/testing.adoc
+++ b/spring-kafka-docs/src/main/antora/modules/ROOT/pages/testing.adoc
@@ -56,8 +56,7 @@ If this is not possible for some reason, note that the `consumeFromEmbeddedTopic
 Since it does not have access to the consumer properties, you must use the overloaded method that takes a `seekToEnd` boolean parameter to seek to the end instead of the beginning.
 ====
 
-NOTE: The `EmbeddedKafkaRule` JUnit 4 rule has been removed in version 4.0.
-For JUnit 4, you should use the `EmbeddedKafkaKraftBroker` directly or migrate to JUnit 5 with the `@EmbeddedKafka` annotation.
+NOTE: `Spring Kafka` no longer supports JUnit4. Please consider migrating from JUnit4 to JUnit5.
 
 The `EmbeddedKafkaBroker` class has a utility method that lets you consume for all the topics it created.
 The following example shows how to use it:
@@ -121,7 +120,7 @@ The following example configuration creates topics called `cat` and `hat` with f
 
 [source, java]
 ----
-@ExtendWith(SpringExtension.class)
+@SpringJUnitConfig
 @EmbeddedKafka(
 		partitions = 5,
 		topics = {"cat", "hat"}
@@ -229,7 +228,7 @@ The following example shows how to use it:
 
 [source, java]
 ----
-@ExtendWith(SpringExtension.class)
+@SpringJUnitConfig
 @DirtiesContext
 @EmbeddedKafka(partitions = 1,
          topics = {
@@ -292,7 +291,7 @@ In addition, the broker properties are loaded from the `broker.properties` class
 Property placeholders are resolved for the `brokerPropertiesLocation` URL and for any property placeholders found in the resource.
 Properties defined by `brokerProperties` override properties found in `brokerPropertiesLocation`.
 
-You can use the `@EmbeddedKafka` annotation with JUnit 4 or JUnit 5.
+You can use the `@EmbeddedKafka` annotation with JUnit 5.
 
 [[embedded-kafka-junit5]]
 == `@EmbeddedKafka` Annotation with JUnit5
@@ -337,7 +336,7 @@ The following example shows how to do so:
 =====
 [source, java]
 ----
-@ExtendWith(SpringExtension.class)
+@SpringJUnitConfig
 @SpringBootTest(properties = "spring.autoconfigure.exclude="
     + "org.springframework.cloud.stream.test.binder.TestSupportBinderAutoConfiguration")
 public class MyApplicationTests {
@@ -351,73 +350,8 @@ There are several ways to use an embedded broker in a Spring Boot application te
 
 They include:
 
-* xref:testing.adoc#kafka-testing-junit4-class-rule[JUnit4 Class Rule]
 * xref:testing.adoc#kafka-testing-embeddedkafka-annotation[`@EmbeddedKafka` Annotation or `EmbeddedKafkaBroker` Bean]
 
-[[kafka-testing-junit4-embedded-broker]]
-=== Junit4 Embedded Broker
-
-The following example shows how to create an embedded broker in Junit4:
-[source, java]
-----
-@SpringBootTest
-public class MyApplicationTests {
-
-    @Autowired
-    private final EmbeddedKafkaBroker broker;
-
-    @Autowired
-    private KafkaTemplate template;
-
-    @Test
-    public void test() {
-        ...
-    }
-
-    @Configuration
-    public static class MyConfiguration {
-        @Bean
-        public EmbeddedKafkaBroker embeddedKafkaBroker() {
-            return new EmbeddedKafkaKraftBroker(1, 1, "someTopic");
-        }
-
-    }
-
-}
-----
-
-[[kafka-testing-junit4-class-rule]]
-=== JUnit4 Class Rule
-
-The following example shows how to use a JUnit4 class rule to create an embedded broker:
-
-[source, java]
-----
-@RunWith(SpringRunner.class)
-@SpringBootTest
-public class MyApplicationTests {
-
-    @ClassRule
-    public static EmbeddedKafkaRule broker = new EmbeddedKafkaRule(1, false, "someTopic")
-            .brokerListProperty("spring.kafka.bootstrap-servers");
-
-    @Autowired
-    private KafkaTemplate template;
-
-    @Test
-    public void test() {
-        ...
-    }
-
-}
-----
-
-Notice that, since this is a Spring Boot application, we override the broker list property to set Spring Boot's property.
-
-NOTE: The `EmbeddedKafkaRule` JUnit 4 rule has been removed in version 4.0.
-For JUnit 4, you should use the `EmbeddedKafkaKraftBroker` directly or migrate to JUnit 5 with the `@EmbeddedKafka` annotation.
-Please refer to xref:kafka-testing-junit4-embedded-broker[Junit4 Embedded Broker]
-
 [[embedded-broker-with-springjunitconfig-annotations]]
 == `@EmbeddedKafka` with `@SpringJunitConfig`
 
@@ -435,7 +369,7 @@ The following example shows how to use an `@EmbeddedKafka` Annotation to create
 
 [source, java]
 ----
-@ExtendWith(SpringExtension.class)
+@SpringJUnitConfig
 @EmbeddedKafka(topics = "someTopic",
         bootstrapServersProperty = "spring.kafka.bootstrap-servers") // this is now the default
 public class MyApplicationTests {
diff --git a/spring-kafka-test/src/main/java/org/springframework/kafka/test/context/EmbeddedKafka.java b/spring-kafka-test/src/main/java/org/springframework/kafka/test/context/EmbeddedKafka.java
index e6d6a0c499..01d6200ccb 100644
--- a/spring-kafka-test/src/main/java/org/springframework/kafka/test/context/EmbeddedKafka.java
+++ b/spring-kafka-test/src/main/java/org/springframework/kafka/test/context/EmbeddedKafka.java
@@ -43,7 +43,7 @@
  * 

* The typical usage of this annotation is like: *

- * @ExtendWith(SpringExtension.class)
+ * @SpringJUnitConfig
  * @EmbeddedKafka
  * public class MyKafkaTests {
  *
diff --git a/spring-kafka-test/src/main/java/org/springframework/kafka/test/rule/Log4j2LevelAdjuster.java b/spring-kafka-test/src/main/java/org/springframework/kafka/test/rule/Log4j2LevelAdjuster.java
index 1a90dca7f2..dc8b7eb78a 100755
--- a/spring-kafka-test/src/main/java/org/springframework/kafka/test/rule/Log4j2LevelAdjuster.java
+++ b/spring-kafka-test/src/main/java/org/springframework/kafka/test/rule/Log4j2LevelAdjuster.java
@@ -1,5 +1,5 @@
 /*
- * Copyright 2002-2019 the original author or authors.
+ * Copyright 2002-2025 the original author or authors.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -38,10 +38,10 @@
  * @author Artem Bilan
  * @author Gary Russell
  * @author Sanghyeok An
- * @deprecated since Spring for Apache Kafka 4.0 in favor of the
- * {@link org.springframework.kafka.test.condition.LogLevels} and JUnit Jupiter.
+ *
+ * @deprecated since 4.0 in favor of {@link org.springframework.kafka.test.condition.LogLevels}.
  */
-@Deprecated(since = "4.0")
+@Deprecated(since = "4.0", forRemoval = true)
 public class Log4j2LevelAdjuster implements MethodRule {
 
 	private final List> classes;

From 7ea24ac01c4a0da3b5af07f6aea0b52286e51df0 Mon Sep 17 00:00:00 2001
From: chickenchickenlove 
Date: Sat, 3 May 2025 10:52:58 +0900
Subject: [PATCH 6/7] Addressing PR review

Signed-off-by: chickenchickenlove 
---
 .../antora/modules/ROOT/pages/testing.adoc    | 20 +++++++++++--------
 1 file changed, 12 insertions(+), 8 deletions(-)

diff --git a/spring-kafka-docs/src/main/antora/modules/ROOT/pages/testing.adoc b/spring-kafka-docs/src/main/antora/modules/ROOT/pages/testing.adoc
index 9c8c37cc45..d7c8269bb7 100644
--- a/spring-kafka-docs/src/main/antora/modules/ROOT/pages/testing.adoc
+++ b/spring-kafka-docs/src/main/antora/modules/ROOT/pages/testing.adoc
@@ -56,7 +56,11 @@ If this is not possible for some reason, note that the `consumeFromEmbeddedTopic
 Since it does not have access to the consumer properties, you must use the overloaded method that takes a `seekToEnd` boolean parameter to seek to the end instead of the beginning.
 ====
 
-NOTE: `Spring Kafka` no longer supports JUnit4. Please consider migrating from JUnit4 to JUnit5.
+[NOTE]
+====
+`Spring Kafka` no longer supports `JUnit4`.
+Migration to `JUnit Jupiter` is recommended.
+====
 
 The `EmbeddedKafkaBroker` class has a utility method that lets you consume for all the topics it created.
 The following example shows how to use it:
@@ -122,8 +126,8 @@ The following example configuration creates topics called `cat` and `hat` with f
 ----
 @SpringJUnitConfig
 @EmbeddedKafka(
-		partitions = 5,
-		topics = {"cat", "hat"}
+        partitions = 5,
+        topics = {"cat", "hat"}
 )
 public class MyTests {
 
@@ -209,7 +213,7 @@ In addition, these properties can be provided:
 
 Essentially these properties mimic some of the `@EmbeddedKafka` attributes.
 
-See more information about configuration properties and how to provide them in the https://junit.org/junit5/docs/current/user-guide/#running-tests-config-params[JUnit 5 User Guide].
+See more information about configuration properties and how to provide them in the https://junit.org/junit5/docs/current/user-guide/#running-tests-config-params[JUnit Jupiter User Guide].
 For example, a `spring.embedded.kafka.brokers.property=my.bootstrap-servers` entry can be added into a `junit-platform.properties` file in the testing classpath.
 Starting with version 3.0.10, the broker automatically sets this to `spring.kafka.bootstrap-servers`, by default, for testing with Spring Boot applications.
 
@@ -291,12 +295,12 @@ In addition, the broker properties are loaded from the `broker.properties` class
 Property placeholders are resolved for the `brokerPropertiesLocation` URL and for any property placeholders found in the resource.
 Properties defined by `brokerProperties` override properties found in `brokerPropertiesLocation`.
 
-You can use the `@EmbeddedKafka` annotation with JUnit 5.
+You can use the `@EmbeddedKafka` annotation with JUnit Jupiter.
 
-[[embedded-kafka-junit5]]
-== `@EmbeddedKafka` Annotation with JUnit5
+[[embedded-kafka-junit-jupiter]]
+== `@EmbeddedKafka` Annotation with JUnit Jupiter
 
-Starting with version 2.3, there are two ways to use the `@EmbeddedKafka` annotation with JUnit5.
+Starting with version 2.3, there are two ways to use the `@EmbeddedKafka` annotation with JUnit Jupiter.
 When used with the `@SpringJunitConfig` annotation, the embedded broker is added to the test application context.
 You can auto wire the broker into your test, at the class or method level, to get the broker address list.
 

From f48c8cda49da0053c971e80435ac65fd3dc237e8 Mon Sep 17 00:00:00 2001
From: chickenchickenlove 
Date: Sat, 3 May 2025 13:05:42 +0900
Subject: [PATCH 7/7] Addressing PR review

Signed-off-by: chickenchickenlove 
---
 .../src/main/antora/modules/ROOT/pages/testing.adoc           | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/spring-kafka-docs/src/main/antora/modules/ROOT/pages/testing.adoc b/spring-kafka-docs/src/main/antora/modules/ROOT/pages/testing.adoc
index d7c8269bb7..5948e41d1e 100644
--- a/spring-kafka-docs/src/main/antora/modules/ROOT/pages/testing.adoc
+++ b/spring-kafka-docs/src/main/antora/modules/ROOT/pages/testing.adoc
@@ -58,8 +58,8 @@ Since it does not have access to the consumer properties, you must use the overl
 
 [NOTE]
 ====
-`Spring Kafka` no longer supports `JUnit4`.
-Migration to `JUnit Jupiter` is recommended.
+Spring for Apache Kafka no longer supports JUnit 4.
+Migration to JUnit Jupiter is recommended.
 ====
 
 The `EmbeddedKafkaBroker` class has a utility method that lets you consume for all the topics it created.