1818
1919import java .util .Arrays ;
2020
21+ import org .springframework .beans .factory .NoSuchBeanDefinitionException ;
2122import org .springframework .context .ApplicationContext ;
22- import org .springframework .core .annotation .AnnotatedElementUtils ;
2323import org .springframework .integration .endpoint .AbstractEndpoint ;
2424import org .springframework .test .context .TestContext ;
25+ import org .springframework .test .context .TestContextAnnotationUtils ;
2526import org .springframework .test .context .TestExecutionListener ;
2627import 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