Change enforceOverride flag to false in @TestBean and @MockitoBean
Based on feedback from the Spring Boot team, we have decided to change the default values for the enforceOverride flags in @TestBean and @MockitoBean from true to false. Closes gh-33613
This commit is contained in:
@@ -40,7 +40,7 @@ public class MockitoBeanJupiterTests {
|
||||
/**
|
||||
* Mock for nonexistent bean.
|
||||
*/
|
||||
@MockitoBean(enforceOverride = false)
|
||||
@MockitoBean
|
||||
GreetingService greetingService;
|
||||
|
||||
/**
|
||||
|
||||
@@ -420,7 +420,7 @@ class BeanOverrideBeanFactoryPostProcessorTests {
|
||||
static class CaseByNameWithQualifier {
|
||||
|
||||
@Qualifier("preferThis")
|
||||
@TestBean(name = "descriptionBean", enforceOverride = false)
|
||||
@TestBean(name = "descriptionBean")
|
||||
private String description;
|
||||
|
||||
static String descriptionBean() {
|
||||
|
||||
@@ -39,7 +39,7 @@ import static org.assertj.core.api.Assertions.assertThat;
|
||||
@SpringJUnitConfig
|
||||
public class TestBeanForByTypeLookupIntegrationTests {
|
||||
|
||||
@TestBean(enforceOverride = false)
|
||||
@TestBean
|
||||
MessageService messageService;
|
||||
|
||||
@TestBean
|
||||
|
||||
@@ -126,7 +126,7 @@ public class TestBeanTests {
|
||||
|
||||
static class FailureByTypeLookup {
|
||||
|
||||
@TestBean
|
||||
@TestBean(enforceOverride = true)
|
||||
private String example;
|
||||
|
||||
static String example() {
|
||||
@@ -136,7 +136,7 @@ public class TestBeanTests {
|
||||
|
||||
static class FailureByNameLookup {
|
||||
|
||||
@TestBean(name = "beanToOverride")
|
||||
@TestBean(name = "beanToOverride", enforceOverride = true)
|
||||
private String example;
|
||||
|
||||
static String example() {
|
||||
|
||||
@@ -48,10 +48,10 @@ public class MockitoBeanForByNameLookupIntegrationTests {
|
||||
@MockitoBean(name = "nestedField")
|
||||
ExampleService renamed2;
|
||||
|
||||
@MockitoBean(name = "nonExistingBean", enforceOverride = false)
|
||||
@MockitoBean(name = "nonExistingBean")
|
||||
ExampleService nonExisting1;
|
||||
|
||||
@MockitoBean(name = "nestedNonExistingBean", enforceOverride = false)
|
||||
@MockitoBean(name = "nestedNonExistingBean")
|
||||
ExampleService nonExisting2;
|
||||
|
||||
|
||||
|
||||
@@ -48,10 +48,10 @@ import static org.mockito.Mockito.verifyNoMoreInteractions;
|
||||
@SpringJUnitConfig
|
||||
public class MockitoBeanForByTypeLookupIntegrationTests {
|
||||
|
||||
@MockitoBean(enforceOverride = false)
|
||||
@MockitoBean
|
||||
AnotherService serviceIsNotABean;
|
||||
|
||||
@MockitoBean(enforceOverride = false)
|
||||
@MockitoBean
|
||||
ExampleService anyNameForService;
|
||||
|
||||
@MockitoBean
|
||||
|
||||
@@ -93,7 +93,7 @@ class MockitoBeanSettingsStrictIntegrationTests {
|
||||
@DirtiesContext
|
||||
static class ImplicitStrictnessWithMockitoBean extends BaseCase {
|
||||
|
||||
@MockitoBean(enforceOverride = false)
|
||||
@MockitoBean
|
||||
@SuppressWarnings("unused")
|
||||
DateTimeFormatter ignoredMock;
|
||||
}
|
||||
|
||||
@@ -29,27 +29,71 @@ import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
|
||||
* Tests for {@link MockitoBean}.
|
||||
*
|
||||
* @author Stephane Nicoll
|
||||
* @author Sam Brannen
|
||||
*/
|
||||
class MockitoMockBeanTests {
|
||||
|
||||
@Test
|
||||
void contextCustomizerCannotBeCreatedWithTooManyCandidates() {
|
||||
void cannotOverrideBeanByNameWithNoSuchBeanName() {
|
||||
GenericApplicationContext context = new GenericApplicationContext();
|
||||
context.registerBean("anotherBean", String.class, () -> "example");
|
||||
BeanOverrideContextCustomizerTestUtils.customizeApplicationContext(FailureByNameLookup.class, context);
|
||||
assertThatIllegalStateException()
|
||||
.isThrownBy(context::refresh)
|
||||
.withMessage("""
|
||||
Unable to override bean: there is no bean definition \
|
||||
to replace with name [beanToOverride] and type [java.lang.String].""");
|
||||
}
|
||||
|
||||
@Test
|
||||
void cannotOverrideBeanByNameWithBeanOfWrongType() {
|
||||
GenericApplicationContext context = new GenericApplicationContext();
|
||||
context.registerBean("beanToOverride", Integer.class, () -> 42);
|
||||
BeanOverrideContextCustomizerTestUtils.customizeApplicationContext(FailureByNameLookup.class, context);
|
||||
assertThatIllegalStateException()
|
||||
.isThrownBy(context::refresh)
|
||||
.withMessage("""
|
||||
Unable to override bean: there is no bean definition \
|
||||
to replace with name [beanToOverride] and type [java.lang.String].""");
|
||||
}
|
||||
|
||||
@Test
|
||||
void cannotOverrideBeanByTypeWithNoSuchBeanType() {
|
||||
GenericApplicationContext context = new GenericApplicationContext();
|
||||
BeanOverrideContextCustomizerTestUtils.customizeApplicationContext(FailureByTypeLookup.class, context);
|
||||
assertThatIllegalStateException()
|
||||
.isThrownBy(context::refresh)
|
||||
.withMessage("""
|
||||
Unable to override bean: no bean definitions of \
|
||||
type %s (as required by annotated field '%s.example')""".formatted(
|
||||
String.class.getName(), FailureByTypeLookup.class.getSimpleName()));
|
||||
}
|
||||
|
||||
@Test
|
||||
void cannotOverrideBeanByTypeWithTooManyBeansOfThatType() {
|
||||
GenericApplicationContext context = new GenericApplicationContext();
|
||||
context.registerBean("bean1", String.class, () -> "example1");
|
||||
context.registerBean("bean2", String.class, () -> "example2");
|
||||
BeanOverrideContextCustomizerTestUtils.customizeApplicationContext(ByTypeSingleLookup.class, context);
|
||||
BeanOverrideContextCustomizerTestUtils.customizeApplicationContext(FailureByTypeLookup.class, context);
|
||||
assertThatIllegalStateException()
|
||||
.isThrownBy(context::refresh)
|
||||
.withMessage("""
|
||||
Unable to select a bean definition to override: found 2 bean definitions \
|
||||
of type %s (as required by annotated field '%s.example'): %s""",
|
||||
String.class.getName(), ByTypeSingleLookup.class.getSimpleName(), List.of("bean1", "bean2"));
|
||||
of type %s (as required by annotated field '%s.example'): %s""".formatted(
|
||||
String.class.getName(), FailureByTypeLookup.class.getSimpleName(), List.of("bean1", "bean2")));
|
||||
}
|
||||
|
||||
|
||||
static class ByTypeSingleLookup {
|
||||
static class FailureByTypeLookup {
|
||||
|
||||
@MockitoBean
|
||||
@MockitoBean(enforceOverride = true)
|
||||
String example;
|
||||
|
||||
}
|
||||
|
||||
static class FailureByNameLookup {
|
||||
|
||||
@MockitoBean(name = "beanToOverride", enforceOverride = true)
|
||||
String example;
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user