Skip to content

Commit 529b5d0

Browse files
GH-3873: Deprecate JUnit 4 utilities in the project
Fixes: #3873 Signed-off-by: chickenchickenlove <[email protected]>
1 parent 70bb92f commit 529b5d0

File tree

4 files changed

+30
-50
lines changed

4 files changed

+30
-50
lines changed

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

Lines changed: 23 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,11 @@ If this is not possible for some reason, note that the `consumeFromEmbeddedTopic
5656
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.
5757
====
5858

59-
NOTE: The `EmbeddedKafkaRule` JUnit 4 rule has been removed in version 4.0.
60-
For JUnit 4, you should use the `EmbeddedKafkaKraftBroker` directly or migrate to JUnit 5 with the `@EmbeddedKafka` annotation.
59+
[NOTE]
60+
====
61+
Spring for Apache Kafka no longer supports JUnit 4.
62+
Migration to JUnit Jupiter is recommended.
63+
====
6164

6265
The `EmbeddedKafkaBroker` class has a utility method that lets you consume for all the topics it created.
6366
The following example shows how to use it:
@@ -121,15 +124,19 @@ The following example configuration creates topics called `cat` and `hat` with f
121124

122125
[source, java]
123126
----
127+
@SpringJUnitConfig
128+
@EmbeddedKafka(
129+
partitions = 5,
130+
topics = {"cat", "hat"}
131+
)
124132
public class MyTests {
125133
126-
@ClassRule
127-
private static EmbeddedKafkaRule embeddedKafka = new EmbeddedKafkaRule(1, false, 5, "cat", "hat");
134+
@Autowired
135+
private EmbeddedKafkaBroker broker;
128136
129137
@Test
130138
public void test() {
131-
embeddedKafkaRule.getEmbeddedKafka()
132-
.addTopics(new NewTopic("thing1", 10, (short) 1), new NewTopic("thing2", 15, (short) 1));
139+
broker.addTopics(new NewTopic("thing1", 10, (short) 1), new NewTopic("thing2", 15, (short) 1));
133140
...
134141
}
135142
@@ -206,7 +213,7 @@ In addition, these properties can be provided:
206213

207214
Essentially these properties mimic some of the `@EmbeddedKafka` attributes.
208215

209-
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].
216+
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].
210217
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.
211218
Starting with version 3.0.10, the broker automatically sets this to `spring.kafka.bootstrap-servers`, by default, for testing with Spring Boot applications.
212219

@@ -225,7 +232,7 @@ The following example shows how to use it:
225232

226233
[source, java]
227234
----
228-
@RunWith(SpringRunner.class)
235+
@SpringJUnitConfig
229236
@DirtiesContext
230237
@EmbeddedKafka(partitions = 1,
231238
topics = {
@@ -237,7 +244,7 @@ public class KafkaStreamsTests {
237244
private EmbeddedKafkaBroker embeddedKafka;
238245
239246
@Test
240-
public void someTest() {
247+
void someTest() {
241248
Map<String, Object> consumerProps = KafkaTestUtils.consumerProps("testGroup", "true", this.embeddedKafka);
242249
consumerProps.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "earliest");
243250
ConsumerFactory<Integer, String> cf = new DefaultKafkaConsumerFactory<>(consumerProps);
@@ -288,12 +295,12 @@ In addition, the broker properties are loaded from the `broker.properties` class
288295
Property placeholders are resolved for the `brokerPropertiesLocation` URL and for any property placeholders found in the resource.
289296
Properties defined by `brokerProperties` override properties found in `brokerPropertiesLocation`.
290297

291-
You can use the `@EmbeddedKafka` annotation with JUnit 4 or JUnit 5.
298+
You can use the `@EmbeddedKafka` annotation with JUnit Jupiter.
292299

293-
[[embedded-kafka-junit5]]
294-
== `@EmbeddedKafka` Annotation with JUnit5
300+
[[embedded-kafka-junit-jupiter]]
301+
== `@EmbeddedKafka` Annotation with JUnit Jupiter
295302

296-
Starting with version 2.3, there are two ways to use the `@EmbeddedKafka` annotation with JUnit5.
303+
Starting with version 2.3, there are two ways to use the `@EmbeddedKafka` annotation with JUnit Jupiter.
297304
When used with the `@SpringJunitConfig` annotation, the embedded broker is added to the test application context.
298305
You can auto wire the broker into your test, at the class or method level, to get the broker address list.
299306

@@ -333,7 +340,7 @@ The following example shows how to do so:
333340
=====
334341
[source, java]
335342
----
336-
@RunWith(SpringRunner.class)
343+
@SpringJUnitConfig
337344
@SpringBootTest(properties = "spring.autoconfigure.exclude="
338345
+ "org.springframework.cloud.stream.test.binder.TestSupportBinderAutoConfiguration")
339346
public class MyApplicationTests {
@@ -347,37 +354,8 @@ There are several ways to use an embedded broker in a Spring Boot application te
347354

348355
They include:
349356

350-
* xref:testing.adoc#kafka-testing-junit4-class-rule[JUnit4 Class Rule]
351357
* xref:testing.adoc#kafka-testing-embeddedkafka-annotation[`@EmbeddedKafka` Annotation or `EmbeddedKafkaBroker` Bean]
352358

353-
[[kafka-testing-junit4-class-rule]]
354-
=== JUnit4 Class Rule
355-
356-
The following example shows how to use a JUnit4 class rule to create an embedded broker:
357-
358-
[source, java]
359-
----
360-
@RunWith(SpringRunner.class)
361-
@SpringBootTest
362-
public class MyApplicationTests {
363-
364-
@ClassRule
365-
public static EmbeddedKafkaRule broker = new EmbeddedKafkaRule(1, false, "someTopic")
366-
.brokerListProperty("spring.kafka.bootstrap-servers");
367-
368-
@Autowired
369-
private KafkaTemplate<String, String> template;
370-
371-
@Test
372-
public void test() {
373-
...
374-
}
375-
376-
}
377-
----
378-
379-
Notice that, since this is a Spring Boot application, we override the broker list property to set Spring Boot's property.
380-
381359
[[embedded-broker-with-springjunitconfig-annotations]]
382360
== `@EmbeddedKafka` with `@SpringJunitConfig`
383361

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

396374
[source, java]
397375
----
398-
@RunWith(SpringRunner.class)
376+
@SpringJUnitConfig
399377
@EmbeddedKafka(topics = "someTopic",
400378
bootstrapServersProperty = "spring.kafka.bootstrap-servers") // this is now the default
401379
public class MyApplicationTests {
@@ -404,7 +382,7 @@ public class MyApplicationTests {
404382
private KafkaTemplate<String, String> template;
405383
406384
@Test
407-
public void test() {
385+
void test() {
408386
...
409387
}
410388

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;SpringJUnitConfig
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
@@ -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+
* @author Sanghyeok An
4041
*
42+
* @deprecated since 4.0 in favor of {@link org.springframework.kafka.test.condition.LogLevels}.
4143
*/
44+
@Deprecated(since = "4.0", forRemoval = true)
4245
public class Log4j2LevelAdjuster implements MethodRule {
4346

4447
private final List<Class<?>> classes;
Lines changed: 1 addition & 2 deletions
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;
@@ -32,7 +32,6 @@
3232
import org.springframework.beans.factory.annotation.Autowired;
3333
import org.springframework.context.annotation.Bean;
3434
import org.springframework.context.annotation.Configuration;
35-
import org.springframework.kafka.test.EmbeddedKafkaKraftBroker;
3635
import org.springframework.kafka.test.utils.KafkaTestUtils;
3736
import org.springframework.test.annotation.DirtiesContext;
3837
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;

0 commit comments

Comments
 (0)