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
pull/19976/head
Stephane Nicoll 5 years ago
parent e92e818b7c
commit eb852f1ad6

@ -1,5 +1,5 @@
/* /*
* Copyright 2012-2019 the original author or authors. * Copyright 2012-2020 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -35,7 +35,8 @@ import static org.assertj.core.api.Assertions.assertThat;
*/ */
class WebMvcEndpointChildContextConfigurationTests { class WebMvcEndpointChildContextConfigurationTests {
private WebApplicationContextRunner contextRunner = new WebApplicationContextRunner(); private WebApplicationContextRunner contextRunner = new WebApplicationContextRunner()
.withAllowBeanDefinitionOverriding(true);
@Test @Test
void contextShouldConfigureRequestContextFilter() { void contextShouldConfigureRequestContextFilter() {

@ -1,5 +1,5 @@
/* /*
* Copyright 2012-2019 the original author or authors. * Copyright 2012-2020 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -136,8 +136,8 @@ class ConditionalOnBeanTests {
@Test @Test
void conditionEvaluationConsidersChangeInTypeWhenBeanIsOverridden() { void conditionEvaluationConsidersChangeInTypeWhenBeanIsOverridden() {
this.contextRunner.withUserConfiguration(OriginalDefinition.class, OverridingDefinition.class, this.contextRunner.withAllowBeanDefinitionOverriding(true).withUserConfiguration(OriginalDefinition.class,
ConsumingConfiguration.class).run((context) -> { OverridingDefinition.class, ConsumingConfiguration.class).run((context) -> {
assertThat(context).hasBean("testBean"); assertThat(context).hasBean("testBean");
assertThat(context).hasSingleBean(Integer.class); assertThat(context).hasSingleBean(Integer.class);
assertThat(context).doesNotHaveBean(ConsumingConfiguration.class); assertThat(context).doesNotHaveBean(ConsumingConfiguration.class);

@ -1,5 +1,5 @@
/* /*
* Copyright 2012-2019 the original author or authors. * Copyright 2012-2020 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -25,7 +25,9 @@ import java.util.function.Supplier;
import org.springframework.beans.factory.config.BeanDefinition; import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.BeanDefinitionCustomizer; import org.springframework.beans.factory.config.BeanDefinitionCustomizer;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.BeanNameGenerator; import org.springframework.beans.factory.support.BeanNameGenerator;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.boot.context.annotation.Configurations; import org.springframework.boot.context.annotation.Configurations;
import org.springframework.boot.context.annotation.UserConfigurations; import org.springframework.boot.context.annotation.UserConfigurations;
import org.springframework.boot.test.context.FilteredClassLoader; import org.springframework.boot.test.context.FilteredClassLoader;
@ -105,6 +107,8 @@ public abstract class AbstractApplicationContextRunner<SELF extends AbstractAppl
private final Supplier<C> contextFactory; private final Supplier<C> contextFactory;
private final boolean allowBeanDefinitionOverriding;
private final List<ApplicationContextInitializer<? super C>> initializers; private final List<ApplicationContextInitializer<? super C>> initializers;
private final TestPropertyValues environmentProperties; private final TestPropertyValues environmentProperties;
@ -124,13 +128,14 @@ public abstract class AbstractApplicationContextRunner<SELF extends AbstractAppl
* @param contextFactory the factory used to create the actual context * @param contextFactory the factory used to create the actual context
*/ */
protected AbstractApplicationContextRunner(Supplier<C> contextFactory) { protected AbstractApplicationContextRunner(Supplier<C> contextFactory) {
this(contextFactory, Collections.emptyList(), TestPropertyValues.empty(), TestPropertyValues.empty(), null, this(contextFactory, false, Collections.emptyList(), TestPropertyValues.empty(), TestPropertyValues.empty(),
null, Collections.emptyList(), Collections.emptyList()); null, null, Collections.emptyList(), Collections.emptyList());
} }
/** /**
* Create a new {@link AbstractApplicationContextRunner} instance. * Create a new {@link AbstractApplicationContextRunner} instance.
* @param contextFactory the factory used to create the actual context * @param contextFactory the factory used to create the actual context
* @param allowBeanDefinitionOverriding whether bean definition overriding is allowed
* @param initializers the initializers * @param initializers the initializers
* @param environmentProperties the environment properties * @param environmentProperties the environment properties
* @param systemProperties the system properties * @param systemProperties the system properties
@ -139,7 +144,7 @@ public abstract class AbstractApplicationContextRunner<SELF extends AbstractAppl
* @param beanRegistrations the bean registrations * @param beanRegistrations the bean registrations
* @param configurations the configuration * @param configurations the configuration
*/ */
protected AbstractApplicationContextRunner(Supplier<C> contextFactory, protected AbstractApplicationContextRunner(Supplier<C> contextFactory, boolean allowBeanDefinitionOverriding,
List<ApplicationContextInitializer<? super C>> initializers, TestPropertyValues environmentProperties, List<ApplicationContextInitializer<? super C>> initializers, TestPropertyValues environmentProperties,
TestPropertyValues systemProperties, ClassLoader classLoader, ApplicationContext parent, TestPropertyValues systemProperties, ClassLoader classLoader, ApplicationContext parent,
List<BeanRegistration<?>> beanRegistrations, List<Configurations> configurations) { List<BeanRegistration<?>> beanRegistrations, List<Configurations> configurations) {
@ -149,6 +154,7 @@ public abstract class AbstractApplicationContextRunner<SELF extends AbstractAppl
Assert.notNull(configurations, "Configurations must not be null"); Assert.notNull(configurations, "Configurations must not be null");
Assert.notNull(initializers, "Initializers must not be null"); Assert.notNull(initializers, "Initializers must not be null");
this.contextFactory = contextFactory; this.contextFactory = contextFactory;
this.allowBeanDefinitionOverriding = allowBeanDefinitionOverriding;
this.initializers = Collections.unmodifiableList(initializers); this.initializers = Collections.unmodifiableList(initializers);
this.environmentProperties = environmentProperties; this.environmentProperties = environmentProperties;
this.systemProperties = systemProperties; this.systemProperties = systemProperties;
@ -159,15 +165,30 @@ public abstract class AbstractApplicationContextRunner<SELF extends AbstractAppl
} }
/** /**
* Add a {@link ApplicationContextInitializer} to be called when the context is * Specify if bean definition overriding, by registering a definition with the same
* created. * name as an existing definition, should be allowed.
* @param allowBeanDefinitionOverriding if bean overriding is allowed
* @return a new instance with the updated bean definition overriding policy
* @since 2.3.0
* @see DefaultListableBeanFactory#setAllowBeanDefinitionOverriding(boolean)
*/
public SELF withAllowBeanDefinitionOverriding(boolean allowBeanDefinitionOverriding) {
return newInstance(this.contextFactory, allowBeanDefinitionOverriding, this.initializers,
this.environmentProperties, this.systemProperties, this.classLoader, this.parent,
this.beanRegistrations, this.configurations);
}
/**
* Add a {@link ApplicationContextInitializer} to be
* calWebMvcEndpointChildContextConfigurationTestsled when the context is created.
* @param initializer the initializer to add * @param initializer the initializer to add
* @return a new instance with the updated initializers * @return a new instance with the updated initializers
*/ */
public SELF withInitializer(ApplicationContextInitializer<? super C> initializer) { public SELF withInitializer(ApplicationContextInitializer<? super C> initializer) {
Assert.notNull(initializer, "Initializer must not be null"); Assert.notNull(initializer, "Initializer must not be null");
return newInstance(this.contextFactory, add(this.initializers, initializer), this.environmentProperties, return newInstance(this.contextFactory, this.allowBeanDefinitionOverriding, add(this.initializers, initializer),
this.systemProperties, this.classLoader, this.parent, this.beanRegistrations, this.configurations); this.environmentProperties, this.systemProperties, this.classLoader, this.parent,
this.beanRegistrations, this.configurations);
} }
/** /**
@ -181,8 +202,9 @@ public abstract class AbstractApplicationContextRunner<SELF extends AbstractAppl
* @see #withSystemProperties(String...) * @see #withSystemProperties(String...)
*/ */
public SELF withPropertyValues(String... pairs) { public SELF withPropertyValues(String... pairs) {
return newInstance(this.contextFactory, this.initializers, this.environmentProperties.and(pairs), return newInstance(this.contextFactory, this.allowBeanDefinitionOverriding, this.initializers,
this.systemProperties, this.classLoader, this.parent, this.beanRegistrations, this.configurations); this.environmentProperties.and(pairs), this.systemProperties, this.classLoader, this.parent,
this.beanRegistrations, this.configurations);
} }
/** /**
@ -196,9 +218,9 @@ public abstract class AbstractApplicationContextRunner<SELF extends AbstractAppl
* @see #withSystemProperties(String...) * @see #withSystemProperties(String...)
*/ */
public SELF withSystemProperties(String... pairs) { public SELF withSystemProperties(String... pairs) {
return newInstance(this.contextFactory, this.initializers, this.environmentProperties, return newInstance(this.contextFactory, this.allowBeanDefinitionOverriding, this.initializers,
this.systemProperties.and(pairs), this.classLoader, this.parent, this.beanRegistrations, this.environmentProperties, this.systemProperties.and(pairs), this.classLoader, this.parent,
this.configurations); this.beanRegistrations, this.configurations);
} }
/** /**
@ -209,8 +231,9 @@ public abstract class AbstractApplicationContextRunner<SELF extends AbstractAppl
* @see FilteredClassLoader * @see FilteredClassLoader
*/ */
public SELF withClassLoader(ClassLoader classLoader) { public SELF withClassLoader(ClassLoader classLoader) {
return newInstance(this.contextFactory, this.initializers, this.environmentProperties, this.systemProperties, return newInstance(this.contextFactory, this.allowBeanDefinitionOverriding, this.initializers,
classLoader, this.parent, this.beanRegistrations, this.configurations); this.environmentProperties, this.systemProperties, classLoader, this.parent, this.beanRegistrations,
this.configurations);
} }
/** /**
@ -220,8 +243,9 @@ public abstract class AbstractApplicationContextRunner<SELF extends AbstractAppl
* @return a new instance with the updated parent * @return a new instance with the updated parent
*/ */
public SELF withParent(ApplicationContext parent) { public SELF withParent(ApplicationContext parent) {
return newInstance(this.contextFactory, this.initializers, this.environmentProperties, this.systemProperties, return newInstance(this.contextFactory, this.allowBeanDefinitionOverriding, this.initializers,
this.classLoader, parent, this.beanRegistrations, this.configurations); this.environmentProperties, this.systemProperties, this.classLoader, parent, this.beanRegistrations,
this.configurations);
} }
/** /**
@ -256,8 +280,8 @@ public abstract class AbstractApplicationContextRunner<SELF extends AbstractAppl
* @return a new instance with the updated bean * @return a new instance with the updated bean
*/ */
public <T> SELF withBean(String name, Class<T> type, Object... constructorArgs) { public <T> SELF withBean(String name, Class<T> type, Object... constructorArgs) {
return newInstance(this.contextFactory, this.initializers, this.environmentProperties, this.systemProperties, return newInstance(this.contextFactory, this.allowBeanDefinitionOverriding, this.initializers,
this.classLoader, this.parent, this.environmentProperties, this.systemProperties, this.classLoader, this.parent,
add(this.beanRegistrations, new BeanRegistration<>(name, type, constructorArgs)), this.configurations); add(this.beanRegistrations, new BeanRegistration<>(name, type, constructorArgs)), this.configurations);
} }
@ -296,8 +320,8 @@ public abstract class AbstractApplicationContextRunner<SELF extends AbstractAppl
*/ */
public <T> SELF withBean(String name, Class<T> type, Supplier<T> supplier, public <T> SELF withBean(String name, Class<T> type, Supplier<T> supplier,
BeanDefinitionCustomizer... customizers) { BeanDefinitionCustomizer... customizers) {
return newInstance(this.contextFactory, this.initializers, this.environmentProperties, this.systemProperties, return newInstance(this.contextFactory, this.allowBeanDefinitionOverriding, this.initializers,
this.classLoader, this.parent, this.environmentProperties, this.systemProperties, this.classLoader, this.parent,
add(this.beanRegistrations, new BeanRegistration<>(name, type, supplier, customizers)), add(this.beanRegistrations, new BeanRegistration<>(name, type, supplier, customizers)),
this.configurations); this.configurations);
} }
@ -319,8 +343,9 @@ public abstract class AbstractApplicationContextRunner<SELF extends AbstractAppl
*/ */
public SELF withConfiguration(Configurations configurations) { public SELF withConfiguration(Configurations configurations) {
Assert.notNull(configurations, "Configurations must not be null"); Assert.notNull(configurations, "Configurations must not be null");
return newInstance(this.contextFactory, this.initializers, this.environmentProperties, this.systemProperties, return newInstance(this.contextFactory, this.allowBeanDefinitionOverriding, this.initializers,
this.classLoader, this.parent, this.beanRegistrations, add(this.configurations, configurations)); this.environmentProperties, this.systemProperties, this.classLoader, this.parent,
this.beanRegistrations, add(this.configurations, configurations));
} }
/** /**
@ -339,7 +364,7 @@ public abstract class AbstractApplicationContextRunner<SELF extends AbstractAppl
return result; return result;
} }
protected abstract SELF newInstance(Supplier<C> contextFactory, protected abstract SELF newInstance(Supplier<C> contextFactory, boolean allowBeanDefinitionOverriding,
List<ApplicationContextInitializer<? super C>> initializers, TestPropertyValues environmentProperties, List<ApplicationContextInitializer<? super C>> initializers, TestPropertyValues environmentProperties,
TestPropertyValues systemProperties, ClassLoader classLoader, ApplicationContext parent, TestPropertyValues systemProperties, ClassLoader classLoader, ApplicationContext parent,
List<BeanRegistration<?>> beanRegistrations, List<Configurations> configurations); List<BeanRegistration<?>> beanRegistrations, List<Configurations> configurations);
@ -388,6 +413,11 @@ public abstract class AbstractApplicationContextRunner<SELF extends AbstractAppl
private C createAndLoadContext() { private C createAndLoadContext() {
C context = this.contextFactory.get(); C context = this.contextFactory.get();
ConfigurableListableBeanFactory beanFactory = context.getBeanFactory();
if (beanFactory instanceof DefaultListableBeanFactory) {
((DefaultListableBeanFactory) beanFactory)
.setAllowBeanDefinitionOverriding(this.allowBeanDefinitionOverriding);
}
try { try {
configureContext(context); configureContext(context);
return context; return context;

@ -1,5 +1,5 @@
/* /*
* Copyright 2012-2019 the original author or authors. * Copyright 2012-2020 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -59,22 +59,24 @@ public class ApplicationContextRunner extends
} }
private ApplicationContextRunner(Supplier<ConfigurableApplicationContext> contextFactory, private ApplicationContextRunner(Supplier<ConfigurableApplicationContext> contextFactory,
boolean allowBeanDefinitionOverriding,
List<ApplicationContextInitializer<? super ConfigurableApplicationContext>> initializers, List<ApplicationContextInitializer<? super ConfigurableApplicationContext>> initializers,
TestPropertyValues environmentProperties, TestPropertyValues systemProperties, ClassLoader classLoader, TestPropertyValues environmentProperties, TestPropertyValues systemProperties, ClassLoader classLoader,
ApplicationContext parent, List<BeanRegistration<?>> beanRegistrations, ApplicationContext parent, List<BeanRegistration<?>> beanRegistrations,
List<Configurations> configurations) { List<Configurations> configurations) {
super(contextFactory, initializers, environmentProperties, systemProperties, classLoader, parent, super(contextFactory, allowBeanDefinitionOverriding, initializers, environmentProperties, systemProperties,
beanRegistrations, configurations); classLoader, parent, beanRegistrations, configurations);
} }
@Override @Override
protected ApplicationContextRunner newInstance(Supplier<ConfigurableApplicationContext> contextFactory, protected ApplicationContextRunner newInstance(Supplier<ConfigurableApplicationContext> contextFactory,
boolean allowBeanDefinitionOverriding,
List<ApplicationContextInitializer<? super ConfigurableApplicationContext>> initializers, List<ApplicationContextInitializer<? super ConfigurableApplicationContext>> initializers,
TestPropertyValues environmentProperties, TestPropertyValues systemProperties, ClassLoader classLoader, TestPropertyValues environmentProperties, TestPropertyValues systemProperties, ClassLoader classLoader,
ApplicationContext parent, List<BeanRegistration<?>> beanRegistrations, ApplicationContext parent, List<BeanRegistration<?>> beanRegistrations,
List<Configurations> configurations) { List<Configurations> configurations) {
return new ApplicationContextRunner(contextFactory, initializers, environmentProperties, systemProperties, return new ApplicationContextRunner(contextFactory, allowBeanDefinitionOverriding, initializers,
classLoader, parent, beanRegistrations, configurations); environmentProperties, systemProperties, classLoader, parent, beanRegistrations, configurations);
} }
} }

@ -1,5 +1,5 @@
/* /*
* Copyright 2012-2019 the original author or authors. * Copyright 2012-2020 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -59,23 +59,24 @@ public final class ReactiveWebApplicationContextRunner extends
} }
private ReactiveWebApplicationContextRunner(Supplier<ConfigurableReactiveWebApplicationContext> contextFactory, private ReactiveWebApplicationContextRunner(Supplier<ConfigurableReactiveWebApplicationContext> contextFactory,
boolean allowBeanDefinitionOverriding,
List<ApplicationContextInitializer<? super ConfigurableReactiveWebApplicationContext>> initializers, List<ApplicationContextInitializer<? super ConfigurableReactiveWebApplicationContext>> initializers,
TestPropertyValues environmentProperties, TestPropertyValues systemProperties, ClassLoader classLoader, TestPropertyValues environmentProperties, TestPropertyValues systemProperties, ClassLoader classLoader,
ApplicationContext parent, List<BeanRegistration<?>> beanRegistrations, ApplicationContext parent, List<BeanRegistration<?>> beanRegistrations,
List<Configurations> configurations) { List<Configurations> configurations) {
super(contextFactory, initializers, environmentProperties, systemProperties, classLoader, parent, super(contextFactory, allowBeanDefinitionOverriding, initializers, environmentProperties, systemProperties,
beanRegistrations, configurations); classLoader, parent, beanRegistrations, configurations);
} }
@Override @Override
protected ReactiveWebApplicationContextRunner newInstance( protected ReactiveWebApplicationContextRunner newInstance(
Supplier<ConfigurableReactiveWebApplicationContext> contextFactory, Supplier<ConfigurableReactiveWebApplicationContext> contextFactory, boolean allowBeanDefinitionOverriding,
List<ApplicationContextInitializer<? super ConfigurableReactiveWebApplicationContext>> initializers, List<ApplicationContextInitializer<? super ConfigurableReactiveWebApplicationContext>> initializers,
TestPropertyValues environmentProperties, TestPropertyValues systemProperties, ClassLoader classLoader, TestPropertyValues environmentProperties, TestPropertyValues systemProperties, ClassLoader classLoader,
ApplicationContext parent, List<BeanRegistration<?>> beanRegistrations, ApplicationContext parent, List<BeanRegistration<?>> beanRegistrations,
List<Configurations> configurations) { List<Configurations> configurations) {
return new ReactiveWebApplicationContextRunner(contextFactory, initializers, environmentProperties, return new ReactiveWebApplicationContextRunner(contextFactory, allowBeanDefinitionOverriding, initializers,
systemProperties, classLoader, parent, beanRegistrations, configurations); environmentProperties, systemProperties, classLoader, parent, beanRegistrations, configurations);
} }
} }

@ -1,5 +1,5 @@
/* /*
* Copyright 2012-2019 the original author or authors. * Copyright 2012-2020 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -63,22 +63,24 @@ public final class WebApplicationContextRunner extends
} }
private WebApplicationContextRunner(Supplier<ConfigurableWebApplicationContext> contextFactory, private WebApplicationContextRunner(Supplier<ConfigurableWebApplicationContext> contextFactory,
boolean allowBeanDefinitionOverriding,
List<ApplicationContextInitializer<? super ConfigurableWebApplicationContext>> initializers, List<ApplicationContextInitializer<? super ConfigurableWebApplicationContext>> initializers,
TestPropertyValues environmentProperties, TestPropertyValues systemProperties, ClassLoader classLoader, TestPropertyValues environmentProperties, TestPropertyValues systemProperties, ClassLoader classLoader,
ApplicationContext parent, List<BeanRegistration<?>> beanRegistrations, ApplicationContext parent, List<BeanRegistration<?>> beanRegistrations,
List<Configurations> configurations) { List<Configurations> configurations) {
super(contextFactory, initializers, environmentProperties, systemProperties, classLoader, parent, super(contextFactory, allowBeanDefinitionOverriding, initializers, environmentProperties, systemProperties,
beanRegistrations, configurations); classLoader, parent, beanRegistrations, configurations);
} }
@Override @Override
protected WebApplicationContextRunner newInstance(Supplier<ConfigurableWebApplicationContext> contextFactory, protected WebApplicationContextRunner newInstance(Supplier<ConfigurableWebApplicationContext> contextFactory,
boolean allowBeanDefinitionOverriding,
List<ApplicationContextInitializer<? super ConfigurableWebApplicationContext>> initializers, List<ApplicationContextInitializer<? super ConfigurableWebApplicationContext>> initializers,
TestPropertyValues environmentProperties, TestPropertyValues systemProperties, ClassLoader classLoader, TestPropertyValues environmentProperties, TestPropertyValues systemProperties, ClassLoader classLoader,
ApplicationContext parent, List<BeanRegistration<?>> beanRegistrations, ApplicationContext parent, List<BeanRegistration<?>> beanRegistrations,
List<Configurations> configurations) { List<Configurations> configurations) {
return new WebApplicationContextRunner(contextFactory, initializers, environmentProperties, systemProperties, return new WebApplicationContextRunner(contextFactory, allowBeanDefinitionOverriding, initializers,
classLoader, parent, beanRegistrations, configurations); environmentProperties, systemProperties, classLoader, parent, beanRegistrations, configurations);
} }
/** /**

@ -23,6 +23,7 @@ import java.util.concurrent.atomic.AtomicBoolean;
import com.google.gson.Gson; import com.google.gson.Gson;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.BeanDefinitionStoreException;
import org.springframework.boot.context.annotation.UserConfigurations; import org.springframework.boot.context.annotation.UserConfigurations;
import org.springframework.boot.test.context.FilteredClassLoader; import org.springframework.boot.test.context.FilteredClassLoader;
import org.springframework.boot.test.context.assertj.ApplicationContextAssertProvider; 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")); 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 @Test
void runWithMultipleConfigurationsShouldRegisterAllConfigurations() { void runWithMultipleConfigurationsShouldRegisterAllConfigurations() {
get().withUserConfiguration(FooConfig.class).withConfiguration(UserConfigurations.of(BarConfig.class)) get().withUserConfiguration(FooConfig.class).withConfiguration(UserConfigurations.of(BarConfig.class))
@ -188,6 +171,34 @@ abstract class AbstractApplicationContextRunnerTests<T extends AbstractApplicati
.withMessageContaining("Expected message")); .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(); protected abstract T get();
private static void throwCheckedException(String message) throws IOException { private static void throwCheckedException(String message) throws IOException {

Loading…
Cancel
Save