From 529b5d0331dc0872bf96db74119e0c9b5bad68e4 Mon Sep 17 00:00:00 2001 From: ChickenchickenLove Date: Mon, 5 May 2025 23:25:45 +0900 Subject: [PATCH] GH-3873: Deprecate JUnit 4 utilities in the project Fixes: https://github.com/spring-projects/spring-kafka/issues/3873 Signed-off-by: chickenchickenlove --- .../antora/modules/ROOT/pages/testing.adoc | 68 +++++++------------ .../kafka/test/context/EmbeddedKafka.java | 4 +- .../kafka/test/rule/Log4j2LevelAdjuster.java | 5 +- .../AddressableEmbeddedBrokerTests.java | 3 +- 4 files changed, 30 insertions(+), 50 deletions(-) rename spring-kafka-test/src/test/java/org/springframework/kafka/test/{rule => }/AddressableEmbeddedBrokerTests.java (97%) 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 b4d64388..5948e41d 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,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: 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 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. The following example shows how to use it: @@ -121,15 +124,19 @@ The following example configuration creates topics called `cat` and `hat` with f [source, java] ---- +@SpringJUnitConfig +@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)); ... } @@ -206,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. @@ -225,7 +232,7 @@ The following example shows how to use it: [source, java] ---- -@RunWith(SpringRunner.class) +@SpringJUnitConfig @DirtiesContext @EmbeddedKafka(partitions = 1, topics = { @@ -237,7 +244,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); @@ -288,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 4 or 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. @@ -333,7 +340,7 @@ The following example shows how to do so: ===== [source, java] ---- -@RunWith(SpringRunner.class) +@SpringJUnitConfig @SpringBootTest(properties = "spring.autoconfigure.exclude=" + "org.springframework.cloud.stream.test.binder.TestSupportBinderAutoConfiguration") public class MyApplicationTests { @@ -347,37 +354,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-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. - [[embedded-broker-with-springjunitconfig-annotations]] == `@EmbeddedKafka` with `@SpringJunitConfig` @@ -395,7 +373,7 @@ The following example shows how to use an `@EmbeddedKafka` Annotation to create [source, java] ---- -@RunWith(SpringRunner.class) +@SpringJUnitConfig @EmbeddedKafka(topics = "someTopic", bootstrapServersProperty = "spring.kafka.bootstrap-servers") // this is now the default public class MyApplicationTests { @@ -404,7 +382,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 1e35a71e..01d6200c 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 @@ import org.springframework.test.context.aot.DisabledInAotMode; *

* The typical usage of this annotation is like: *

- * @RunWith(SpringRunner.class)
+ * @SpringJUnitConfig
  * @EmbeddedKafka
  * public class MyKafkaTests {
  *
@@ -67,6 +67,7 @@ import org.springframework.test.context.aot.DisabledInAotMode;
  * @author Pawel Lozinski
  * @author Adrian Chlebosz
  * @author Soby Chacko
+ * @author Sanghyeok An
  *
  * @since 1.3
  *
@@ -169,4 +170,3 @@ public @interface EmbeddedKafka {
 	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 31fd8bd9..dc8b7eb7 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.
@@ -37,8 +37,11 @@ import org.springframework.kafka.test.utils.JUnitUtils.LevelsContainer;
  * @author Dave Syer
  * @author Artem Bilan
  * @author Gary Russell
+ * @author Sanghyeok An
  *
+ * @deprecated since 4.0 in favor of {@link org.springframework.kafka.test.condition.LogLevels}.
  */
+@Deprecated(since = "4.0", forRemoval = true)
 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 97%
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 3b73ef81..8c58e651 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;
@@ -32,7 +32,6 @@ import org.junit.jupiter.api.Test;
 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;