Skip to content

Commit

Permalink
Ensure @ContextHierarchy is never used with UseMainMethod
Browse files Browse the repository at this point in the history
Fixes gh-33078
  • Loading branch information
philwebb committed Nov 17, 2022
1 parent b9e57c7 commit fafbefa
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
Expand Up @@ -149,6 +149,8 @@ private Method getMainMethod(MergedContextConfiguration mergedConfig, UseMainMet
if (useMainMethod == UseMainMethod.NEVER) {
return null;
}
Assert.state(mergedConfig.getParent() == null,
() -> "UseMainMethod.%s cannot be used with @ContextHierarchy tests".formatted(useMainMethod));
Class<?> springBootConfiguration = Arrays.stream(mergedConfig.getClasses())
.filter(this::isSpringBootConfiguration).findFirst().orElse(null);
Assert.state(springBootConfiguration != null || useMainMethod == UseMainMethod.WHEN_AVAILABLE,
Expand Down
Expand Up @@ -39,6 +39,8 @@
import org.springframework.core.env.StandardEnvironment;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.ApplicationContextFailureProcessor;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.ContextHierarchy;
import org.springframework.test.context.MergedContextConfiguration;
import org.springframework.test.context.TestContext;
import org.springframework.test.context.TestContextManager;
Expand Down Expand Up @@ -232,6 +234,14 @@ void whenNoMainMethodWithBeanThrowingException() {
assertThat(ContextLoaderApplicationContextFailureProcessor.failedContext).isNotNull();
}

@Test
void whenUseMainMethodWithContextHierarchyThrowsException() {
TestContext testContext = new ExposedTestContextManager(UseMainMethodWithContextHierarchy.class)
.getExposedTestContext();
assertThatIllegalStateException().isThrownBy(testContext::getApplicationContext).havingCause()
.withMessage("UseMainMethod.ALWAYS cannot be used with @ContextHierarchy tests");
}

private String[] getActiveProfiles(Class<?> testClass) {
TestContext testContext = new ExposedTestContextManager(testClass).getExposedTestContext();
ApplicationContext applicationContext = testContext.getApplicationContext();
Expand Down Expand Up @@ -349,6 +359,13 @@ static class NoMainMethodWithBeanThrowingException {

}

@SpringBootTest(useMainMethod = UseMainMethod.ALWAYS)
@ContextHierarchy({ @ContextConfiguration(classes = ConfigWithMain.class),
@ContextConfiguration(classes = AnotherConfigWithMain.class) })
static class UseMainMethodWithContextHierarchy {

}

@Configuration(proxyBeanMethods = false)
static class Config {

Expand All @@ -363,6 +380,15 @@ public static void main(String[] args) {

}

@SpringBootConfiguration(proxyBeanMethods = false)
public static class AnotherConfigWithMain {

public static void main(String[] args) {
new SpringApplication(AnotherConfigWithMain.class).run("--spring.profiles.active=anotherfrommain");
}

}

@SpringBootConfiguration(proxyBeanMethods = false)
static class ConfigWithNoMain {

Expand Down

0 comments on commit fafbefa

Please sign in to comment.