diff --git a/spring-integration-test/src/main/java/org/springframework/integration/test/context/MockIntegrationContextCustomizerFactory.java b/spring-integration-test/src/main/java/org/springframework/integration/test/context/MockIntegrationContextCustomizerFactory.java index a29d2eca965..29387bb60da 100644 --- a/spring-integration-test/src/main/java/org/springframework/integration/test/context/MockIntegrationContextCustomizerFactory.java +++ b/spring-integration-test/src/main/java/org/springframework/integration/test/context/MockIntegrationContextCustomizerFactory.java @@ -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 @@ 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. + *

+ * 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 configAttributes) { - return AnnotatedElementUtils.hasAnnotation(testClass, SpringIntegrationTest.class) + return TestContextAnnotationUtils.hasAnnotation(testClass, SpringIntegrationTest.class) ? new MockIntegrationContextCustomizer() : null; } diff --git a/spring-integration-test/src/main/java/org/springframework/integration/test/context/SpringIntegrationTest.java b/spring-integration-test/src/main/java/org/springframework/integration/test/context/SpringIntegrationTest.java index c6f9dd13df8..02bd8d90abe 100644 --- a/spring-integration-test/src/main/java/org/springframework/integration/test/context/SpringIntegrationTest.java +++ b/spring-integration-test/src/main/java/org/springframework/integration/test/context/SpringIntegrationTest.java @@ -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 @@ * * } * + *

+ * Honors the {@link org.springframework.test.context.NestedTestConfiguration} semantics. * * @author Artem Bilan * diff --git a/spring-integration-test/src/main/java/org/springframework/integration/test/context/SpringIntegrationTestExecutionListener.java b/spring-integration-test/src/main/java/org/springframework/integration/test/context/SpringIntegrationTestExecutionListener.java index 836fcec26e2..078dc0af97b 100644 --- a/spring-integration-test/src/main/java/org/springframework/integration/test/context/SpringIntegrationTestExecutionListener.java +++ b/spring-integration-test/src/main/java/org/springframework/integration/test/context/SpringIntegrationTestExecutionListener.java @@ -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 @@ 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]; diff --git a/spring-integration-test/src/test/java/org/springframework/integration/test/context/AbstractIntegrationTest.java b/spring-integration-test/src/test/java/org/springframework/integration/test/context/AbstractIntegrationTest.java new file mode 100644 index 00000000000..466cef50b25 --- /dev/null +++ b/spring-integration-test/src/test/java/org/springframework/integration/test/context/AbstractIntegrationTest.java @@ -0,0 +1,52 @@ +/* + * Copyright 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. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.integration.test.context; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.integration.config.EnableIntegration; +import org.springframework.integration.endpoint.AbstractEndpoint; +import org.springframework.integration.endpoint.ReactiveMessageSourceProducer; +import org.springframework.messaging.support.GenericMessage; +import org.springframework.test.context.junit.jupiter.SpringJUnitConfig; + +/** + * Base integration test that specifies a default {@link SpringIntegrationTest} + * to be inherited by concrete subclasses. + * + * @author Chris Bono + * + * @since 6.2.10 + */ +@SpringJUnitConfig +@SpringIntegrationTest(noAutoStartup = "*") +class AbstractIntegrationTest { + + @Configuration(proxyBeanMethods = false) + @EnableIntegration + static class MockEndpointConfig { + + @Bean + AbstractEndpoint mockEndpoint() { + ReactiveMessageSourceProducer endpoint = + new ReactiveMessageSourceProducer(() -> new GenericMessage<>("testFromMockEndpoint")); + endpoint.setOutputChannelName("nullChannel"); + return endpoint; + } + + } +} diff --git a/spring-integration-test/src/test/java/org/springframework/integration/test/context/NestedSpringIntegrationTestAnnotationTests.java b/spring-integration-test/src/test/java/org/springframework/integration/test/context/NestedSpringIntegrationTestAnnotationTests.java new file mode 100644 index 00000000000..a9c40970c18 --- /dev/null +++ b/spring-integration-test/src/test/java/org/springframework/integration/test/context/NestedSpringIntegrationTestAnnotationTests.java @@ -0,0 +1,95 @@ +/* + * Copyright 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. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.integration.test.context; + +import org.junit.jupiter.api.Nested; +import org.junit.jupiter.api.Test; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.integration.endpoint.AbstractEndpoint; +import org.springframework.lang.Nullable; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.NestedTestConfiguration; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * Concrete specialization of {@link AbstractIntegrationTest} used to + * test inherit and override behavior of {@link SpringIntegrationTest} + * when used with {@link Nested} and {@link NestedTestConfiguration}. + * + * @author Chris Bono + * @author Artem Bilan + * + * @since 6.2.10 + */ +class NestedSpringIntegrationTestAnnotationTests extends AbstractIntegrationTest { + + @Test + void annotationDefinedOnParentIsInheritedByDefault(@Autowired AbstractEndpoint mockEndpoint) { + assertThat(mockEndpoint.isRunning()).isFalse(); + } + + @Nested + class NestedTestDefaultEnclosingConfiguration { + + @Test + void annotationDefinedOnParentOfEnclosingIsInheritedByDefault(@Autowired AbstractEndpoint mockEndpoint) { + assertThat(mockEndpoint.isRunning()).isFalse(); + } + + } + + @Nested + @NestedTestConfiguration(NestedTestConfiguration.EnclosingConfiguration.INHERIT) + class NestedTestWithInheritEnclosingConfiguration { + + @Test + void annotationDefinedOnParentOfEnclosingIsInherited(@Autowired AbstractEndpoint mockEndpoint) { + assertThat(mockEndpoint.isRunning()).isFalse(); + } + + } + + @Nested + @NestedTestConfiguration(NestedTestConfiguration.EnclosingConfiguration.INHERIT) + @SpringIntegrationTest(noAutoStartup = "noSuchEndpointWithThisPatternExists") + class NestedTestWithInheritEnclosingConfigurationButOverrideAnnotation { + + @Test + void annotationDefinedOnParentOfEnclosingIsOverridden(@Autowired AbstractEndpoint mockEndpoint) { + assertThat(mockEndpoint.isRunning()).isTrue(); + } + + } + + @Nested + @NestedTestConfiguration(NestedTestConfiguration.EnclosingConfiguration.OVERRIDE) + @ContextConfiguration(classes = MockEndpointConfig.class) + class NestedTestWithOverrideEnclosingConfiguration { + + @Test + void annotationDefinedOnParentOfEnclosingIsIgnored(@Autowired AbstractEndpoint mockEndpoint, + @Nullable @Autowired MockIntegrationContext mockIntegrationContext) { + + assertThat(mockEndpoint.isRunning()).isTrue(); + assertThat(mockIntegrationContext).isNull(); + } + + } + +} diff --git a/src/reference/antora/modules/ROOT/pages/testing.adoc b/src/reference/antora/modules/ROOT/pages/testing.adoc index 4e6aae22da5..74017affd61 100644 --- a/src/reference/antora/modules/ROOT/pages/testing.adoc +++ b/src/reference/antora/modules/ROOT/pages/testing.adoc @@ -206,6 +206,8 @@ The endpoints are matched to the provided patterns, which support the following This is useful when we would like to not have real connections to the target systems from inbound channel adapters (for example an AMQP Inbound Gateway, JDBC Polling Channel Adapter, WebSocket Message Producer in client mode, and so on). +The `@SpringIntegrationTest` honors the `org.springframework.test.context.NestedTestConfiguration` semantics, hence it can be declared on the outer class (or even its super class) - and `@SpringIntegrationTest` environment will be available to inherited `@Nested` tests. + The `MockIntegrationContext` is meant to be used in the target test cases for modifications to beans in the real application context. For example, endpoints that have `autoStartup` overridden to `false` can be replaced with mocks, as the following example shows: