Closed
Description
Overview
Over the past few weeks it has become apparent that our test suite does not fully cover all features of the Bean Override support in the TestContext framework.
To remedy that, we should introduce tests to cover corner cases and features that are currently not tested. As a starting point, we can adopt several of the tests for @MockBean
and @SpyBean
from Spring Boot's test suite.
Features to Test
The following is a list of features we should make sure we test before Spring Framework 6.2 GA.
- Combining
SpringExtension
andMockitoExtension
on the same test class, including support for@Captor
combined with@MockitoSpyBean
. -
@MockitoBean
,@MockitoSpyBean
, and@TestBean
with@DirtiesContext
"before method" modes. -
@MockitoBean
with JUnit 4 andSpringMethodRule
. -
@MockitoBean
and@MockitoSpyBean
with@ContextHierarchy
. -
@MockitoBean
and@MockitoSpyBean
with generics and parameterized types. -
@MockitoBean
replacement of a bean with@Async
methods. -
@MockitoBean
replacement of a scoped proxy. -
@MockitoBean
replacement of a Spring AOP proxy. -
@MockitoSpyBean
with circular dependencies. - Spring AOP proxy that wraps a Mockito spy created via
@MockitoSpyBean
. -
@MockitoSpyBean
on a field with generics can be used to replace an existing bean with matching generics that's produced by aFactoryBean
that's programmatically registered via anImportBeanDefinitionRegistrar
. -
@MockitoBean
and@MockitoSpyBean
can select a single candidate bean to override when there are multiple candidates that match the required type: via an explicit bean name,@Qualifier
, or@Primary
on one of the candidate beans. - Mock reset support for beans within the
ApplicationContext
configured viaMockReset.before()
andMockReset.after()
.
Related Issues
- Recommend the use of AopTestUtils.getTargetObject() when setting expectations on a spied bean that Spring has proxied spring-boot#22281
- When using
@MockitoBean
and@MockitoSpyBean
make it easier to inject any underlying proxy target as well as the mock or spy #33743 @MockitoBean
,@MockitoSpyBean
, &@TestBean
do not work with@DirtiesContext
"before method" modes #33783- Bean Overrides cannot reliably override beans created by a
FactoryBean
with generics #33811 - Test Bean Overrides do not honor
@Primary
semantics #33819 MockReset
should be honored without@Mockito[Spy]Bean
fields #33829