Skip to content

Commit 2b4b570

Browse files
authored
GH-2692: Fix Embedded Kafka for @nested tests
Fixes #2692 When used with `@nested`, Multiple Contexts appear causing test to break. Contexts caching miss. * Fix Context Customizer Factory to Work with Nested Test * Add spring test with nested class * Fix checkstyle violations
1 parent 206e68f commit 2b4b570

File tree

2 files changed

+75
-3
lines changed

2 files changed

+75
-3
lines changed

spring-kafka-test/src/main/java/org/springframework/kafka/test/context/EmbeddedKafkaContextCustomizerFactory.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2017-2019 the original author or authors.
2+
* Copyright 2017-2023 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.
@@ -18,10 +18,10 @@
1818

1919
import java.util.List;
2020

21-
import org.springframework.core.annotation.AnnotatedElementUtils;
2221
import org.springframework.test.context.ContextConfigurationAttributes;
2322
import org.springframework.test.context.ContextCustomizer;
2423
import org.springframework.test.context.ContextCustomizerFactory;
24+
import org.springframework.test.context.TestContextAnnotationUtils;
2525

2626
/**
2727
* The {@link ContextCustomizerFactory} implementation to produce a
@@ -38,7 +38,7 @@ class EmbeddedKafkaContextCustomizerFactory implements ContextCustomizerFactory
3838
public ContextCustomizer createContextCustomizer(Class<?> testClass,
3939
List<ContextConfigurationAttributes> configAttributes) {
4040
EmbeddedKafka embeddedKafka =
41-
AnnotatedElementUtils.findMergedAnnotation(testClass, EmbeddedKafka.class);
41+
TestContextAnnotationUtils.findMergedAnnotation(testClass, EmbeddedKafka.class);
4242
return embeddedKafka != null ? new EmbeddedKafkaContextCustomizer(embeddedKafka) : null;
4343
}
4444

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/*
2+
* Copyright 2021-2023 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 static org.assertj.core.api.Assertions.assertThat;
20+
21+
import java.util.List;
22+
import java.util.concurrent.atomic.AtomicInteger;
23+
24+
import org.junit.jupiter.api.Nested;
25+
import org.junit.jupiter.api.Test;
26+
27+
import org.springframework.beans.factory.annotation.Autowired;
28+
import org.springframework.context.annotation.Bean;
29+
import org.springframework.context.annotation.Configuration;
30+
import org.springframework.kafka.test.context.EmbeddedKafka;
31+
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
32+
33+
@EmbeddedKafka
34+
@SpringJUnitConfig(WithNestedClassContextTests.Config.class)
35+
class WithNestedClassContextTests {
36+
37+
private static final AtomicInteger counter = new AtomicInteger();
38+
39+
@Autowired
40+
private TestClass outer;
41+
42+
@Nested
43+
class NestedClass {
44+
45+
@Test
46+
void equalsInjected(@Autowired TestClass inner) {
47+
assertThat(inner).isEqualTo(outer);
48+
}
49+
50+
@Test
51+
void equalsSize(@Autowired List<TestClass> classes) {
52+
assertThat(classes).hasSize(1);
53+
}
54+
55+
@Test
56+
void equalsCount() {
57+
assertThat(counter.get()).isEqualTo(1);
58+
}
59+
}
60+
61+
public static class TestClass {
62+
}
63+
64+
@Configuration
65+
static class Config {
66+
@Bean
67+
public TestClass testClass() {
68+
counter.incrementAndGet();
69+
return new TestClass();
70+
}
71+
}
72+
}

0 commit comments

Comments
 (0)