From 8e863135bf337f4852af7dc30cadf7a00720a2a4 Mon Sep 17 00:00:00 2001 From: Oliver Drotbohm Date: Tue, 1 Aug 2023 23:58:17 +0200 Subject: [PATCH] GH-202 - Let @ApplicationModuleTest be meta-annotated with @SpringBootTest. @ApplicationModuleTest is now meta-annotated with @SpringBootTest. This allows us to remove a couple of declarations that we actually had copied from it (such as the TestContextBootstrapper, the SpringExtension etc.) The presence of the original annotation allow test-related auto-configuration to inspect @SprignBootTest for particular configuration. For example, we now alias the WebEnvironment to make it configurable for the test execution. --- spring-modulith-integration-test/pom.xml | 6 +++++ ...ApplicationModuleTestIntegrationTests.java | 22 +++++++++++++++++++ .../modulith/test/ApplicationModuleTest.java | 17 +++++++++----- 3 files changed, 40 insertions(+), 5 deletions(-) diff --git a/spring-modulith-integration-test/pom.xml b/spring-modulith-integration-test/pom.xml index bed0ab8d..3c529317 100644 --- a/spring-modulith-integration-test/pom.xml +++ b/spring-modulith-integration-test/pom.xml @@ -73,6 +73,12 @@ test + + org.springframework.boot + spring-boot-starter-webflux + test + + diff --git a/spring-modulith-integration-test/src/test/java/com/acme/myproject/moduleA/ApplicationModuleTestIntegrationTests.java b/spring-modulith-integration-test/src/test/java/com/acme/myproject/moduleA/ApplicationModuleTestIntegrationTests.java index 7734269a..d7e6d223 100644 --- a/spring-modulith-integration-test/src/test/java/com/acme/myproject/moduleA/ApplicationModuleTestIntegrationTests.java +++ b/spring-modulith-integration-test/src/test/java/com/acme/myproject/moduleA/ApplicationModuleTestIntegrationTests.java @@ -15,9 +15,16 @@ */ package com.acme.myproject.moduleA; +import static org.assertj.core.api.Assertions.*; + import org.junit.jupiter.api.Nested; import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.web.reactive.AutoConfigureWebTestClient; +import org.springframework.context.ApplicationContext; import org.springframework.modulith.test.ApplicationModuleTest; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.web.reactive.server.WebTestClient; /** * Integration tests for {@link ApplicationModuleTest}. @@ -36,4 +43,19 @@ class SomeNestedClass { @Test // GH-173 void bootstrapsSecondLevelMestMethod() {} } + + // GH-202 + @Nested + @ApplicationModuleTest(verifyAutomatically = false) + @ContextConfiguration + @AutoConfigureWebTestClient + class SampleTest { + + @Autowired ApplicationContext context; + + @Test + void registersTestWebClient() { + assertThat(context.getBean(WebTestClient.class)).isNotNull(); + } + } } diff --git a/spring-modulith-test/src/main/java/org/springframework/modulith/test/ApplicationModuleTest.java b/spring-modulith-test/src/main/java/org/springframework/modulith/test/ApplicationModuleTest.java index 28cf6d65..59f9c3df 100644 --- a/spring-modulith-test/src/main/java/org/springframework/modulith/test/ApplicationModuleTest.java +++ b/spring-modulith-test/src/main/java/org/springframework/modulith/test/ApplicationModuleTest.java @@ -27,13 +27,12 @@ import org.junit.jupiter.api.extension.ExtendWith; import org.springframework.boot.autoconfigure.ImportAutoConfiguration; import org.springframework.boot.test.autoconfigure.filter.TypeExcludeFilters; -import org.springframework.boot.test.context.SpringBootTestContextBootstrapper; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; import org.springframework.core.annotation.AliasFor; import org.springframework.modulith.core.DependencyDepth; -import org.springframework.test.context.BootstrapWith; import org.springframework.test.context.TestConstructor; import org.springframework.test.context.TestConstructor.AutowireMode; -import org.springframework.test.context.junit.jupiter.SpringExtension; /** * Bootstraps the module containing the package of the test class annotated with {@link ApplicationModuleTest}. Will @@ -49,10 +48,10 @@ @Documented @Inherited @Retention(RetentionPolicy.RUNTIME) -@BootstrapWith(SpringBootTestContextBootstrapper.class) +@SpringBootTest @TypeExcludeFilters(ModuleTypeExcludeFilter.class) @ImportAutoConfiguration(ModuleTestAutoConfiguration.class) -@ExtendWith({ SpringExtension.class, PublishedEventsParameterResolver.class, ScenarioParameterResolver.class }) +@ExtendWith({ PublishedEventsParameterResolver.class, ScenarioParameterResolver.class }) @TestInstance(Lifecycle.PER_CLASS) @TestConstructor(autowireMode = AutowireMode.ALL) public @interface ApplicationModuleTest { @@ -77,6 +76,14 @@ */ String[] extraIncludes() default {}; + /** + * The type of web environment to create when applicable. Defaults to {@link WebEnvironment#MOCK}. + * + * @return the type of web environment + */ + @AliasFor(annotation = SpringBootTest.class) + WebEnvironment webEnvironment() default WebEnvironment.MOCK; + public enum BootstrapMode { /**