Currently, @MockitoBean
can be used to create or replace a bean definition, and @TestBean
can only be used to replace a bean definition.
The reason is that the @MockitoBean
support is hard coded to use the REPLACE_OR_CREATE_DEFINITION
strategy; whereas, the @TestBean
support is hard coded to use the REPLACE_DEFINITION
strategy.
Instead of hard coding those strategies, we should introduce 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. TestBeanOverrideProcessor
and MockitoBeanOverrideProcessor
can then dynamically decide whether to use the REPLACE_DEFINITION
or REPLACE_OR_CREATE_DEFINITION
strategy based on the enforceOverride
flag.
Comment From: sbrannen
Reopening to switch the default values for enforceOverride
attributes to false
, to align with the existing behavior for @MockBean
in Spring Boot and to simplify the programming model for common use cases.