Skip to content

Commit 2267430

Browse files
committed
Find service connections declaring in enclosing classes
Fixes gh-34790
1 parent cd5d923 commit 2267430

File tree

3 files changed

+98
-2
lines changed

3 files changed

+98
-2
lines changed

spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/service/connection/ServiceConnectionContextCustomizer.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,4 +78,8 @@ private <T> BeanDefinition createBeanDefinition(T instance) {
7878
return new RootBeanDefinition((Class<T>) instance.getClass(), () -> instance);
7979
}
8080

81+
List<ContainerConnectionSource<?, ?, ?>> getSources() {
82+
return this.sources;
83+
}
84+
8185
}

spring-boot-project/spring-boot-test-autoconfigure/src/main/java/org/springframework/boot/test/autoconfigure/service/connection/ServiceConnectionContextCustomizerFactory.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import org.springframework.test.context.ContextConfigurationAttributes;
2929
import org.springframework.test.context.ContextCustomizer;
3030
import org.springframework.test.context.ContextCustomizerFactory;
31+
import org.springframework.test.context.TestContextAnnotationUtils;
3132
import org.springframework.util.Assert;
3233
import org.springframework.util.ReflectionUtils;
3334

@@ -44,12 +45,19 @@ class ServiceConnectionContextCustomizerFactory implements ContextCustomizerFact
4445
public ContextCustomizer createContextCustomizer(Class<?> testClass,
4546
List<ContextConfigurationAttributes> configAttributes) {
4647
List<ContainerConnectionSource<?, ?, ?>> sources = new ArrayList<>();
47-
ReflectionUtils.doWithFields(testClass, (field) -> {
48+
findSources(testClass, sources);
49+
return (sources.isEmpty()) ? null : new ServiceConnectionContextCustomizer(sources);
50+
}
51+
52+
private void findSources(Class<?> clazz, List<ContainerConnectionSource<?, ?, ?>> sources) {
53+
ReflectionUtils.doWithFields(clazz, (field) -> {
4854
MergedAnnotations annotations = MergedAnnotations.from(field);
4955
annotations.stream(ServiceConnection.class)
5056
.forEach((annotation) -> sources.add(createSource(field, annotation)));
5157
});
52-
return (sources.isEmpty()) ? null : new ServiceConnectionContextCustomizer(sources);
58+
if (TestContextAnnotationUtils.searchEnclosingClass(clazz)) {
59+
findSources(clazz.getEnclosingClass(), sources);
60+
}
5361
}
5462

5563
private ContainerConnectionSource<?, ?, ?> createSource(Field field,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/*
2+
* Copyright 2012-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.boot.test.autoconfigure.service.connection;
18+
19+
import org.junit.jupiter.api.Nested;
20+
import org.junit.jupiter.api.Test;
21+
import org.testcontainers.containers.GenericContainer;
22+
23+
import org.springframework.boot.autoconfigure.service.connection.ConnectionDetails;
24+
import org.springframework.boot.test.autoconfigure.service.connection.ServiceConnectionContextCustomizerFactoryTests.ServiceConnections.NestedClass;
25+
26+
import static org.assertj.core.api.Assertions.assertThat;
27+
28+
/**
29+
* Tests for {@link ServiceConnectionContextCustomizerFactory}.
30+
*
31+
* @author Andy Wilkinson
32+
*/
33+
public class ServiceConnectionContextCustomizerFactoryTests {
34+
35+
private final ServiceConnectionContextCustomizerFactory factory = new ServiceConnectionContextCustomizerFactory();
36+
37+
@Test
38+
void whenClassHasNoServiceConnectionsThenCreateReturnsNull() {
39+
assertThat(this.factory.createContextCustomizer(NoServiceConnections.class, null)).isNull();
40+
}
41+
42+
@Test
43+
void whenClassHasServiceConnectionsThenCreateReturnsCustomizer() {
44+
ServiceConnectionContextCustomizer customizer = (ServiceConnectionContextCustomizer) this.factory
45+
.createContextCustomizer(ServiceConnections.class, null);
46+
assertThat(customizer).isNotNull();
47+
assertThat(customizer.getSources()).hasSize(2);
48+
}
49+
50+
@Test
51+
void whenEnclosingClassHasServiceConnectionsThenCreateReturnsCustomizer() {
52+
ServiceConnectionContextCustomizer customizer = (ServiceConnectionContextCustomizer) this.factory
53+
.createContextCustomizer(NestedClass.class, null);
54+
assertThat(customizer).isNotNull();
55+
assertThat(customizer.getSources()).hasSize(3);
56+
}
57+
58+
static class NoServiceConnections {
59+
60+
}
61+
62+
static class ServiceConnections {
63+
64+
@ServiceConnection(TestConnectionDetails.class)
65+
private static GenericContainer<?> service1 = new GenericContainer<>("example");
66+
67+
@ServiceConnection(TestConnectionDetails.class)
68+
private static GenericContainer<?> service2 = new GenericContainer<>("example");
69+
70+
@Nested
71+
class NestedClass {
72+
73+
@ServiceConnection(TestConnectionDetails.class)
74+
private static GenericContainer<?> service3 = new GenericContainer<>("example");
75+
76+
}
77+
78+
}
79+
80+
static class TestConnectionDetails implements ConnectionDetails {
81+
82+
}
83+
84+
}

0 commit comments

Comments
 (0)