GH-9492: Honor @Nested in @SpringIntegrationTest

Fixes: #9492
Issue link: https://github.com/spring-projects/spring-integration/issues/9492

Switches `MockIntegrationContextCustomizerFactory` & `SpringIntegrationTestExecutionListener`
to use `TestContextAnnotationUtils` in order to properly support
`@NestedTestConfiguration` semantics.

Adds an integration test with a base class and several
`@Nested` inner test classes to verify the various
permutations of `@SpringIntegrationTest` inherit/override
behavior when used w/ `@NestedTestConfiguration`.

**Auto-cherry-pick to `6.3.x` & `6.2.x`**
This commit is contained in:
Martin Tomik
2024-09-23 20:04:00 -04:00
committed by Artem Bilan
parent 8f838d04ce
commit 3a8e3abc7d
6 changed files with 161 additions and 7 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2017-2022 the original author or authors.
* Copyright 2017-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -18,17 +18,20 @@ package org.springframework.integration.test.context;
import java.util.List;
import org.springframework.core.annotation.AnnotatedElementUtils;
import org.springframework.test.context.ContextConfigurationAttributes;
import org.springframework.test.context.ContextCustomizer;
import org.springframework.test.context.ContextCustomizerFactory;
import org.springframework.test.context.TestContextAnnotationUtils;
/**
* The {@link ContextCustomizerFactory} implementation to produce a
* {@link MockIntegrationContextCustomizer} if a {@link SpringIntegrationTest} annotation
* is present on the test class.
* <p>
* Honors the {@link org.springframework.test.context.NestedTestConfiguration} semantics.
*
* @author Artem Bilan
* @author Chris Bono
*
* @since 5.0
*/
@@ -38,7 +41,7 @@ class MockIntegrationContextCustomizerFactory implements ContextCustomizerFactor
public ContextCustomizer createContextCustomizer(Class<?> testClass,
List<ContextConfigurationAttributes> configAttributes) {
return AnnotatedElementUtils.hasAnnotation(testClass, SpringIntegrationTest.class)
return TestContextAnnotationUtils.hasAnnotation(testClass, SpringIntegrationTest.class)
? new MockIntegrationContextCustomizer()
: null;
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2017-2022 the original author or authors.
* Copyright 2017-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -51,6 +51,8 @@ import org.springframework.test.context.TestExecutionListeners;
*
* }
* </pre>
* <p>
* Honors the {@link org.springframework.test.context.NestedTestConfiguration} semantics.
*
* @author Artem Bilan
*

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2017-2022 the original author or authors.
* Copyright 2017-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -19,9 +19,9 @@ package org.springframework.integration.test.context;
import java.util.Arrays;
import org.springframework.context.ApplicationContext;
import org.springframework.core.annotation.AnnotatedElementUtils;
import org.springframework.integration.endpoint.AbstractEndpoint;
import org.springframework.test.context.TestContext;
import org.springframework.test.context.TestContextAnnotationUtils;
import org.springframework.test.context.TestExecutionListener;
import org.springframework.util.PatternMatchUtils;
@@ -39,7 +39,7 @@ class SpringIntegrationTestExecutionListener implements TestExecutionListener {
@Override
public void prepareTestInstance(TestContext testContext) {
SpringIntegrationTest springIntegrationTest =
AnnotatedElementUtils.findMergedAnnotation(testContext.getTestClass(), SpringIntegrationTest.class);
TestContextAnnotationUtils.findMergedAnnotation(testContext.getTestClass(), SpringIntegrationTest.class);
String[] patterns = springIntegrationTest != null ? springIntegrationTest.noAutoStartup() : new String[0];