Skip to content

Commit 3055212

Browse files
committed
GH-253 - 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.
1 parent d665161 commit 3055212

File tree

3 files changed

+40
-5
lines changed

3 files changed

+40
-5
lines changed

spring-modulith-integration-test/pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,12 @@
7373
<scope>test</scope>
7474
</dependency>
7575

76+
<dependency>
77+
<groupId>org.springframework.boot</groupId>
78+
<artifactId>spring-boot-starter-webflux</artifactId>
79+
<scope>test</scope>
80+
</dependency>
81+
7682
</dependencies>
7783

7884
</project>

spring-modulith-integration-test/src/test/java/com/acme/myproject/moduleA/ApplicationModuleTestIntegrationTests.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,16 @@
1515
*/
1616
package com.acme.myproject.moduleA;
1717

18+
import static org.assertj.core.api.Assertions.*;
19+
1820
import org.junit.jupiter.api.Nested;
1921
import org.junit.jupiter.api.Test;
22+
import org.springframework.beans.factory.annotation.Autowired;
23+
import org.springframework.boot.test.autoconfigure.web.reactive.AutoConfigureWebTestClient;
24+
import org.springframework.context.ApplicationContext;
2025
import org.springframework.modulith.test.ApplicationModuleTest;
26+
import org.springframework.test.context.ContextConfiguration;
27+
import org.springframework.test.web.reactive.server.WebTestClient;
2128

2229
/**
2330
* Integration tests for {@link ApplicationModuleTest}.
@@ -36,4 +43,19 @@ class SomeNestedClass {
3643
@Test // GH-173
3744
void bootstrapsSecondLevelMestMethod() {}
3845
}
46+
47+
// GH-253
48+
@Nested
49+
@ApplicationModuleTest(verifyAutomatically = false)
50+
@ContextConfiguration
51+
@AutoConfigureWebTestClient
52+
class SampleTest {
53+
54+
@Autowired ApplicationContext context;
55+
56+
@Test
57+
void registersTestWebClient() {
58+
assertThat(context.getBean(WebTestClient.class)).isNotNull();
59+
}
60+
}
3961
}

spring-modulith-test/src/main/java/org/springframework/modulith/test/ApplicationModuleTest.java

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,12 @@
2727
import org.junit.jupiter.api.extension.ExtendWith;
2828
import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
2929
import org.springframework.boot.test.autoconfigure.filter.TypeExcludeFilters;
30-
import org.springframework.boot.test.context.SpringBootTestContextBootstrapper;
30+
import org.springframework.boot.test.context.SpringBootTest;
31+
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
3132
import org.springframework.core.annotation.AliasFor;
3233
import org.springframework.modulith.core.DependencyDepth;
33-
import org.springframework.test.context.BootstrapWith;
3434
import org.springframework.test.context.TestConstructor;
3535
import org.springframework.test.context.TestConstructor.AutowireMode;
36-
import org.springframework.test.context.junit.jupiter.SpringExtension;
3736

3837
/**
3938
* Bootstraps the module containing the package of the test class annotated with {@link ApplicationModuleTest}. Will
@@ -49,10 +48,10 @@
4948
@Documented
5049
@Inherited
5150
@Retention(RetentionPolicy.RUNTIME)
52-
@BootstrapWith(SpringBootTestContextBootstrapper.class)
51+
@SpringBootTest
5352
@TypeExcludeFilters(ModuleTypeExcludeFilter.class)
5453
@ImportAutoConfiguration(ModuleTestAutoConfiguration.class)
55-
@ExtendWith({ SpringExtension.class, PublishedEventsParameterResolver.class, ScenarioParameterResolver.class })
54+
@ExtendWith({ PublishedEventsParameterResolver.class, ScenarioParameterResolver.class })
5655
@TestInstance(Lifecycle.PER_CLASS)
5756
@TestConstructor(autowireMode = AutowireMode.ALL)
5857
public @interface ApplicationModuleTest {
@@ -77,6 +76,14 @@
7776
*/
7877
String[] extraIncludes() default {};
7978

79+
/**
80+
* The type of web environment to create when applicable. Defaults to {@link WebEnvironment#MOCK}.
81+
*
82+
* @return the type of web environment
83+
*/
84+
@AliasFor(annotation = SpringBootTest.class)
85+
WebEnvironment webEnvironment() default WebEnvironment.MOCK;
86+
8087
public enum BootstrapMode {
8188

8289
/**

0 commit comments

Comments
 (0)