Introduce enforceOverride flag in @⁠TestBean and @⁠MockitoBean

Prior to this commit, @⁠MockitoBean could be used to either create or
replace a bean definition, but @⁠TestBean could only be used to replace
a bean definition.

However, Bean Override implementations should require the presence of
an existing bean definition by default (i.e. literally "override" by
default), while giving the user the option to have a new bean
definition created if desired.

To address that, this commit introduces a new `enforceOverride`
attribute in @⁠TestBean and @⁠MockitoBean that defaults to true but
allows the user to decide if it's OK to create a bean for a nonexistent
bean definition.

Closes gh-33613
This commit is contained in:
Sam Brannen
2024-09-30 14:24:14 +02:00
parent 30dc86810e
commit 1c87e4795d
12 changed files with 103 additions and 31 deletions

View File

@@ -11,6 +11,10 @@ to override. If multiple candidates match, `@Qualifier` can be provided to narro
candidate to override. Alternatively, a candidate whose bean definition name matches the
name of the field will match.
When using `@MockitoBean`, if you would like for a new bean definition to be created when
a corresponding bean definition does not exist, set the `enforceOverride` attribute to
`false` for example, `@MockitoBean(enforceOverride = false)`.
To use a by-name override rather than a by-type override, specify the `name` attribute
of the annotation.

View File

@@ -15,6 +15,10 @@ to override. If multiple candidates match, `@Qualifier` can be provided to narro
candidate to override. Alternatively, a candidate whose bean definition name matches the
name of the field will match.
If you would like for a new bean definition to be created when a corresponding bean
definition does not exist, set the `enforceOverride` attribute to `false` for example,
`@TestBean(enforceOverride = false)`.
To use a by-name override rather than a by-type override, specify the `name` attribute
of the annotation.