Disable bean overriding by default in ApplicationContextRunner

For consistency with SpringApplication, this commit disables bean
overriding by default in ApplicationContextRunner. Bean overriding can
be enabled again using withAllowBeanDefinitionOverriding.

Closes gh-18019
This commit is contained in:
Stephane Nicoll
2019-12-30 11:52:09 +01:00
parent e92e818b7c
commit eb852f1ad6
7 changed files with 110 additions and 63 deletions

View File

@@ -23,6 +23,7 @@ import java.util.concurrent.atomic.AtomicBoolean;
import com.google.gson.Gson;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.BeanDefinitionStoreException;
import org.springframework.boot.context.annotation.UserConfigurations;
import org.springframework.boot.test.context.FilteredClassLoader;
import org.springframework.boot.test.context.assertj.ApplicationContextAssertProvider;
@@ -139,24 +140,6 @@ abstract class AbstractApplicationContextRunnerTests<T extends AbstractApplicati
get().withBean(String.class, () -> "foo").run((context) -> assertThat(context).hasBean("string"));
}
@Test
void runWithUserBeanShouldBeRegisteredInOrder() {
get().withBean(String.class, () -> "one").withBean(String.class, () -> "two")
.withBean(String.class, () -> "three").run((context) -> {
assertThat(context).hasBean("string");
assertThat(context.getBean("string")).isEqualTo("three");
});
}
@Test
void runWithConfigurationsAndUserBeanShouldRegisterUserBeanLast() {
get().withUserConfiguration(FooConfig.class).withBean("foo", String.class, () -> "overridden")
.run((context) -> {
assertThat(context).hasBean("foo");
assertThat(context.getBean("foo")).isEqualTo("overridden");
});
}
@Test
void runWithMultipleConfigurationsShouldRegisterAllConfigurations() {
get().withUserConfiguration(FooConfig.class).withConfiguration(UserConfigurations.of(BarConfig.class))
@@ -188,6 +171,34 @@ abstract class AbstractApplicationContextRunnerTests<T extends AbstractApplicati
.withMessageContaining("Expected message"));
}
@Test
void runDisablesBeanOverridingByDefault() {
get().withUserConfiguration(FooConfig.class).withBean("foo", Integer.class, () -> 42).run((context) -> {
assertThat(context).hasFailed();
assertThat(context.getStartupFailure()).isInstanceOf(BeanDefinitionStoreException.class)
.hasMessageContaining("Invalid bean definition with name 'foo'")
.hasMessageContaining("@Bean definition illegally overridden by existing bean definition");
});
}
@Test
void runWithUserBeanShouldBeRegisteredInOrder() {
get().withAllowBeanDefinitionOverriding(true).withBean(String.class, () -> "one")
.withBean(String.class, () -> "two").withBean(String.class, () -> "three").run((context) -> {
assertThat(context).hasBean("string");
assertThat(context.getBean("string")).isEqualTo("three");
});
}
@Test
void runWithConfigurationsAndUserBeanShouldRegisterUserBeanLast() {
get().withAllowBeanDefinitionOverriding(true).withUserConfiguration(FooConfig.class)
.withBean("foo", String.class, () -> "overridden").run((context) -> {
assertThat(context).hasBean("foo");
assertThat(context.getBean("foo")).isEqualTo("overridden");
});
}
protected abstract T get();
private static void throwCheckedException(String message) throws IOException {