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:
@@ -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.
|
||||
|
||||
|
||||
@@ -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.
|
||||
|
||||
|
||||
Reference in New Issue
Block a user