Skip to content

Commit abe382b

Browse files
committed
GH-9492: Fix @SpringIntegration in @nested test
1 parent fc37712 commit abe382b

File tree

1 file changed

+24
-4
lines changed

1 file changed

+24
-4
lines changed

spring-integration-test/src/main/java/org/springframework/integration/test/context/SpringIntegrationTestExecutionListener.java

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,11 @@
1818

1919
import java.util.Arrays;
2020

21+
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
2122
import org.springframework.context.ApplicationContext;
22-
import org.springframework.core.annotation.AnnotatedElementUtils;
2323
import org.springframework.integration.endpoint.AbstractEndpoint;
2424
import org.springframework.test.context.TestContext;
25+
import org.springframework.test.context.TestContextAnnotationUtils;
2526
import org.springframework.test.context.TestExecutionListener;
2627
import org.springframework.util.PatternMatchUtils;
2728

@@ -39,12 +40,16 @@ class SpringIntegrationTestExecutionListener implements TestExecutionListener {
3940
@Override
4041
public void prepareTestInstance(TestContext testContext) {
4142
SpringIntegrationTest springIntegrationTest =
42-
AnnotatedElementUtils.findMergedAnnotation(testContext.getTestClass(), SpringIntegrationTest.class);
43+
TestContextAnnotationUtils.findMergedAnnotation(testContext.getTestClass(), SpringIntegrationTest.class);
4344

4445
String[] patterns = springIntegrationTest != null ? springIntegrationTest.noAutoStartup() : new String[0];
4546

4647
ApplicationContext applicationContext = testContext.getApplicationContext();
47-
MockIntegrationContext mockIntegrationContext = applicationContext.getBean(MockIntegrationContext.class);
48+
MockIntegrationContext mockIntegrationContext = handledGetMockIntegrationContext(applicationContext);
49+
if (mockIntegrationContext == null) {
50+
return;
51+
}
52+
4853
mockIntegrationContext.getAutoStartupCandidates()
4954
.stream()
5055
.filter(endpoint -> !match(endpoint.getBeanName(), patterns))
@@ -55,7 +60,11 @@ public void prepareTestInstance(TestContext testContext) {
5560
@Override
5661
public void afterTestClass(TestContext testContext) {
5762
ApplicationContext applicationContext = testContext.getApplicationContext();
58-
MockIntegrationContext mockIntegrationContext = applicationContext.getBean(MockIntegrationContext.class);
63+
MockIntegrationContext mockIntegrationContext = handledGetMockIntegrationContext(applicationContext);
64+
if (mockIntegrationContext == null) {
65+
return;
66+
}
67+
5968
mockIntegrationContext.getAutoStartupCandidates()
6069
.forEach(AbstractEndpoint::stop);
6170
}
@@ -66,4 +75,15 @@ private boolean match(String name, String[] patterns) {
6675
.anyMatch(pattern -> PatternMatchUtils.simpleMatch(pattern, name));
6776
}
6877

78+
/**
79+
* to allow nested class execution
80+
*/
81+
private MockIntegrationContext handledGetMockIntegrationContext(ApplicationContext applicationContext) {
82+
try {
83+
return applicationContext.getBean(MockIntegrationContext.class);
84+
} catch (NoSuchBeanDefinitionException e) {
85+
return null;
86+
}
87+
}
88+
6989
}

0 commit comments

Comments
 (0)