Skip to content

Commit 077adbe

Browse files
Revert "GH-3873: Deprecate JUnit 4 utilities in the project"
This reverts commit 2da4d5d. Signed-off-by: chickenchickenlove <[email protected]>
1 parent 2da4d5d commit 077adbe

File tree

5 files changed

+35
-33
lines changed

5 files changed

+35
-33
lines changed

build.gradle

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ ext {
5757
hibernateValidationVersion = '8.0.2.Final'
5858
jacksonBomVersion = '2.18.3'
5959
jaywayJsonPathVersion = '2.9.0'
60+
junit4Version = '4.13.2'
6061
junitJupiterVersion = '5.12.2'
6162
kafkaVersion = '4.0.0'
6263
kotlinCoroutinesVersion = '1.10.2'
@@ -377,6 +378,9 @@ project ('spring-kafka-test') {
377378
api 'org.junit.platform:junit-platform-launcher'
378379
optionalApi "org.hamcrest:hamcrest-core:$hamcrestVersion"
379380
optionalApi "org.mockito:mockito-core:$mockitoVersion"
381+
optionalApi ("junit:junit:$junit4Version") {
382+
exclude group: 'org.hamcrest', module: 'hamcrest-core'
383+
}
380384
optionalApi "org.apache.logging.log4j:log4j-core:$log4jVersion"
381385
}
382386
}

spring-kafka-test/src/main/java/org/springframework/kafka/test/extensions/package-info.java

Lines changed: 0 additions & 5 deletions
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2025 the original author or authors.
2+
* Copyright 2002-2019 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.
@@ -14,34 +14,32 @@
1414
* limitations under the License.
1515
*/
1616

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

19-
import java.lang.reflect.Method;
2019
import java.util.ArrayList;
2120
import java.util.Arrays;
2221
import java.util.Collections;
2322
import java.util.List;
2423

2524
import org.apache.logging.log4j.Level;
26-
import org.junit.jupiter.api.extension.ExtensionContext;
27-
import org.junit.jupiter.api.extension.InvocationInterceptor;
28-
import org.junit.jupiter.api.extension.ReflectiveInvocationContext;
25+
import org.junit.rules.MethodRule;
26+
import org.junit.runners.model.FrameworkMethod;
27+
import org.junit.runners.model.Statement;
2928

3029
import org.springframework.kafka.test.utils.JUnitUtils;
3130
import org.springframework.kafka.test.utils.JUnitUtils.LevelsContainer;
3231

3332
/**
34-
* A JUnit extension &#064; that changes the logger level for a set of classes
33+
* A JUnit method &#064;Rule that changes the logger level for a set of classes
3534
* while a test method is running. Useful for performance or scalability tests
3635
* where we don't want to generate a large log in a tight inner loop.
3736
*
3837
* @author Dave Syer
3938
* @author Artem Bilan
4039
* @author Gary Russell
41-
* @author Sanghyoek An
4240
*
4341
*/
44-
public class Log4j2LevelAdjuster implements InvocationInterceptor {
42+
public class Log4j2LevelAdjuster implements MethodRule {
4543

4644
private final List<Class<?>> classes;
4745

@@ -62,25 +60,25 @@ public Log4j2LevelAdjuster categories(String... categoriesToAdjust) {
6260
}
6361

6462
@Override
65-
public void interceptTestMethod(Invocation<Void> invocation,
66-
ReflectiveInvocationContext<Method> invocationContext,
67-
ExtensionContext extensionContext) throws Throwable {
68-
String methodName = extensionContext.getRequiredTestMethod().getName();
69-
LevelsContainer container = null;
70-
71-
try {
72-
container = JUnitUtils.adjustLogLevels(
73-
methodName,
74-
Log4j2LevelAdjuster.this.classes,
75-
Log4j2LevelAdjuster.this.categories,
76-
Log4j2LevelAdjuster.this.level);
77-
invocation.proceed();
78-
}
79-
finally {
80-
if (container != null) {
81-
JUnitUtils.revertLevels(methodName, container);
63+
public Statement apply(final Statement base, FrameworkMethod method, Object target) {
64+
return new Statement() {
65+
@Override
66+
public void evaluate() throws Throwable {
67+
LevelsContainer container = null;
68+
try {
69+
container = JUnitUtils.adjustLogLevels(method.getName(),
70+
Log4j2LevelAdjuster.this.classes, Log4j2LevelAdjuster.this.categories,
71+
Log4j2LevelAdjuster.this.level);
72+
base.evaluate();
73+
}
74+
finally {
75+
if (container != null) {
76+
JUnitUtils.revertLevels(method.getName(), container);
77+
}
78+
}
8279
}
83-
}
80+
81+
};
8482
}
8583

8684
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
/**
2+
* Provides JUnit rules.
3+
*/
4+
@org.jspecify.annotations.NullMarked
5+
package org.springframework.kafka.test.rule;
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.extensions;
17+
package org.springframework.kafka.test.rule;
1818

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

0 commit comments

Comments
 (0)