Skip to content

Commit 9766e42

Browse files
garyrussellartembilan
authored andcommitted
Convert all tests to JUnit 5
1 parent 8e4d968 commit 9766e42

File tree

47 files changed

+803
-667
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+803
-667
lines changed

build.gradle

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -123,9 +123,6 @@ subprojects { subproject ->
123123
testRuntime "org.junit.jupiter:junit-jupiter-engine:$junitJupiterVersion"
124124
testRuntime "org.junit.platform:junit-platform-launcher:$junitPlatformVersion"
125125

126-
// To support JUnit 4 tests
127-
testRuntime "org.junit.vintage:junit-vintage-engine:$junitJupiterVersion"
128-
129126
// To avoid compiler warnings about @API annotations in JUnit code
130127
testCompileOnly 'org.apiguardian:apiguardian-api:1.0.0'
131128

@@ -267,7 +264,7 @@ project ('spring-kafka') {
267264
compile ("io.projectreactor:reactor-core:$reactorVersion", optional)
268265
compile ("io.projectreactor.kafka:reactor-kafka:$reactorKafkaVersion", optional)
269266
testCompile ("io.projectreactor:reactor-test:$reactorVersion")
270-
267+
testCompile "org.mockito:mockito-junit-jupiter:$mockitoVersion"
271268
testCompile project (":spring-kafka-test")
272269
testCompile "org.hibernate.validator:hibernate-validator:$hibernateValidationVersion"
273270
}
@@ -294,11 +291,15 @@ project ('spring-kafka-test') {
294291

295292
compile ("org.mockito:mockito-core:$mockitoVersion", optional)
296293

297-
compile ("junit:junit:$junit4Version", optional)
294+
compile ("junit:junit:$junit4Version") {
295+
optional(it)
296+
exclude group: 'org.hamcrest', module: 'hamcrest-core'
297+
}
298298
compile "org.junit.jupiter:junit-jupiter-api:$junitJupiterVersion"
299299

300300
compile ("org.assertj:assertj-core:$assertjVersion", optional)
301301
compile ("org.apache.logging.log4j:log4j-core:$log4jVersion", optional)
302+
302303
}
303304
}
304305

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*
2+
* Copyright 2019 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.kafka.test.condition;
18+
19+
import java.lang.annotation.Documented;
20+
import java.lang.annotation.ElementType;
21+
import java.lang.annotation.Retention;
22+
import java.lang.annotation.RetentionPolicy;
23+
import java.lang.annotation.Target;
24+
25+
import org.junit.jupiter.api.extension.ExtendWith;
26+
27+
/**
28+
* Test classes annotated with this will change logging levels between tests. It can also
29+
* be applied to individual test methods. If both class-level and method-level annotations
30+
* are present, the method-level annotation is used.
31+
*
32+
* @author Gary Russell
33+
* @since 2.2
34+
*
35+
*/
36+
@ExtendWith(LogLevelsCondition.class)
37+
@Target({ ElementType.TYPE, ElementType.METHOD })
38+
@Retention(RetentionPolicy.RUNTIME)
39+
@Documented
40+
public @interface LogLevels {
41+
42+
/**
43+
* Classes representing Log4j categories to change.
44+
* @return the classes.
45+
*/
46+
Class<?>[] classes() default {};
47+
48+
/**
49+
* Category names representing Log4j or Logback categories to change.
50+
* @return the names.
51+
*/
52+
String[] categories() default {};
53+
54+
/**
55+
* The Log4j level name to switch the categories to during the test.
56+
* @return the level (default Log4j {@code Levels.toLevel()} - currently, DEBUG).
57+
*/
58+
String level() default "";
59+
60+
}
Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
/*
2+
* Copyright 2019 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.kafka.test.condition;
18+
19+
import java.lang.reflect.AnnotatedElement;
20+
import java.lang.reflect.Method;
21+
import java.util.Arrays;
22+
import java.util.Map;
23+
import java.util.Optional;
24+
import java.util.concurrent.ConcurrentHashMap;
25+
26+
import org.apache.commons.logging.LogFactory;
27+
import org.apache.logging.log4j.Level;
28+
import org.junit.jupiter.api.extension.AfterAllCallback;
29+
import org.junit.jupiter.api.extension.AfterEachCallback;
30+
import org.junit.jupiter.api.extension.BeforeAllCallback;
31+
import org.junit.jupiter.api.extension.BeforeEachCallback;
32+
import org.junit.jupiter.api.extension.ConditionEvaluationResult;
33+
import org.junit.jupiter.api.extension.ExecutionCondition;
34+
import org.junit.jupiter.api.extension.ExtensionContext;
35+
import org.junit.jupiter.api.extension.ExtensionContext.Namespace;
36+
import org.junit.jupiter.api.extension.ExtensionContext.Store;
37+
38+
import org.springframework.core.annotation.MergedAnnotation;
39+
import org.springframework.core.annotation.MergedAnnotations;
40+
import org.springframework.core.log.LogAccessor;
41+
import org.springframework.kafka.test.utils.JUnitUtils;
42+
import org.springframework.kafka.test.utils.JUnitUtils.LevelsContainer;
43+
44+
/**
45+
* JUnit condition that adjusts and reverts log levels before/after each test.
46+
*
47+
* @author Gary Russell
48+
* @since 2.2
49+
*
50+
*/
51+
public class LogLevelsCondition
52+
implements ExecutionCondition, BeforeEachCallback, AfterEachCallback, BeforeAllCallback, AfterAllCallback {
53+
54+
private static final LogAccessor LOGGER = new LogAccessor(LogFactory.getLog(LogLevelsCondition.class));
55+
56+
private static final String STORE_ANNOTATION_KEY = "logLevelsAnnotation";
57+
58+
private static final String STORE_CONTAINER_KEY = "logLevelsContainer";
59+
60+
private static final ConditionEvaluationResult ENABLED =
61+
ConditionEvaluationResult.enabled("@LogLevels always enabled");
62+
63+
private final Map<String, Boolean> loggedMethods = new ConcurrentHashMap<>();
64+
65+
@Override
66+
public ConditionEvaluationResult evaluateExecutionCondition(ExtensionContext context) {
67+
Optional<AnnotatedElement> element = context.getElement();
68+
MergedAnnotations annotations = MergedAnnotations.from(element.get(),
69+
MergedAnnotations.SearchStrategy.TYPE_HIERARCHY);
70+
MergedAnnotation<LogLevels> mergedAnnotation = annotations.get(LogLevels.class);
71+
if (mergedAnnotation.isPresent()) {
72+
LogLevels loglevels = mergedAnnotation.synthesize();
73+
Store store = context.getStore(Namespace.create(getClass(), context));
74+
store.put(STORE_ANNOTATION_KEY, loglevels);
75+
}
76+
return ENABLED;
77+
}
78+
79+
@Override
80+
public void beforeAll(ExtensionContext context) {
81+
Store store = context.getStore(Namespace.create(getClass(), context));
82+
LogLevels logLevels = store.get(STORE_ANNOTATION_KEY, LogLevels.class);
83+
if (logLevels != null) {
84+
store.put(STORE_CONTAINER_KEY, JUnitUtils.adjustLogLevels(context.getDisplayName(),
85+
Arrays.asList((logLevels.classes())),
86+
Arrays.asList(logLevels.categories()),
87+
Level.toLevel(logLevels.level())));
88+
}
89+
}
90+
91+
@Override
92+
public void beforeEach(ExtensionContext context) {
93+
Store store = context.getStore(Namespace.create(getClass(), context));
94+
LogLevels logLevels = store.get(STORE_ANNOTATION_KEY, LogLevels.class);
95+
if (logLevels != null) { // Method level annotation
96+
if (store.get(STORE_CONTAINER_KEY) == null) {
97+
store.put(STORE_CONTAINER_KEY, JUnitUtils.adjustLogLevels(context.getDisplayName(),
98+
Arrays.asList((logLevels.classes())),
99+
Arrays.asList(logLevels.categories()),
100+
Level.toLevel(logLevels.level())));
101+
}
102+
}
103+
else {
104+
Optional<Method> testMethod = context.getTestMethod();
105+
if (testMethod.isPresent()
106+
&& this.loggedMethods.putIfAbsent(testMethod.get().getName(), Boolean.TRUE) == null) {
107+
LOGGER.info(() -> "+++++++++++++++++++++++++++++ Begin " + testMethod.get().getName());
108+
}
109+
}
110+
}
111+
112+
@Override
113+
public void afterEach(ExtensionContext context) {
114+
Store store = context.getStore(Namespace.create(getClass(), context));
115+
LevelsContainer container = store.get(STORE_CONTAINER_KEY, LevelsContainer.class);
116+
if (container != null) {
117+
LogLevels logLevels = store.get(STORE_ANNOTATION_KEY, LogLevels.class);
118+
if (logLevels != null) {
119+
JUnitUtils.revertLevels(context.getDisplayName(), container);
120+
store.remove(STORE_CONTAINER_KEY);
121+
}
122+
}
123+
}
124+
125+
@Override
126+
public void afterAll(ExtensionContext context) {
127+
Store store = context.getStore(Namespace.create(getClass(), context));
128+
LogLevels logLevels = store.remove(STORE_ANNOTATION_KEY, LogLevels.class);
129+
if (logLevels != null) {
130+
LevelsContainer container = store.get(STORE_CONTAINER_KEY, LevelsContainer.class);
131+
JUnitUtils.revertLevels(context.getDisplayName(), container);
132+
store.remove(STORE_CONTAINER_KEY);
133+
}
134+
this.loggedMethods.clear();
135+
}
136+
137+
}

0 commit comments

Comments
 (0)