You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
OverrideMockitoBeanWithMockitoBeanTests currently fails as follows.
Multiple Failures (3 failures)
-- failure 1 --
[MockReset for service2)]
expected: AFTER
but was: BEFORE
-- failure 2 --
Expected not same: messageService
-- failure 3 --
Expected size: 2 but was: 1 in:
[messageService]
Note, however, that this behavior is not specific to the Bean Override support in Spring Framework: the same behavior exists in Spring Boot for @MockBean and @SpyBean.
Analysis
Now that #34054 has been addressed, we do reject "identical" bean overrides, but we still permit multiple logically equivalent bean overrides.
The challenge lies in figuring out when bean overrides are logically equivalent, and we have to keep the following in mind.
The implementations of equals() and hashCode() in concrete implementations of BeanOverrideHandler (such as TestBeanOverrideHandler and MockitoBeanOverrideHandler) consider two handlers equal based not only on what beans they should override but also on how they should override those beans.
Even if equals() and hashCode() in concrete implementations of BeanOverrideHandler were based solely on what beans they should override, the "phantom read" issue still exists (see aa7b459).
In other words, if we were to change the equals() and hashCode() implementations in handlers, that would allow for some level of static, upfront rejection, but it would still not be possible to reliably predict all cases where a bean override may potentially override another bean override.
Rather, we likely have to track the bean names of all overrides created by BeanOverrideHandlers and ensure that no two handlers attempt to override the same bean.
Proposal
Detect when two bean override handlers attempt to override the same bean and log a warning.
Overview
As mentioned in cd60a00, it is possible for a bean override to override another logically equivalent bean override.
For example, a
@TestBean
can override a@MockitoBean
, and vice versa.In fact, it's also possible for a
@MockitoBean
to override another@MockitoBean
, as can be seen in the following test case.OverrideMockitoBeanWithMockitoBeanTests
currently fails as follows.Note, however, that this behavior is not specific to the Bean Override support in Spring Framework: the same behavior exists in Spring Boot for
@MockBean
and@SpyBean
.Analysis
Now that #34054 has been addressed, we do reject "identical" bean overrides, but we still permit multiple logically equivalent bean overrides.
The challenge lies in figuring out when bean overrides are logically equivalent, and we have to keep the following in mind.
equals()
andhashCode()
in concrete implementations ofBeanOverrideHandler
(such asTestBeanOverrideHandler
andMockitoBeanOverrideHandler
) consider two handlers equal based not only on what beans they should override but also on how they should override those beans.equals()
andhashCode()
in concrete implementations ofBeanOverrideHandler
were based solely on what beans they should override, the "phantom read" issue still exists (see aa7b459).In other words, if we were to change the
equals()
andhashCode()
implementations in handlers, that would allow for some level of static, upfront rejection, but it would still not be possible to reliably predict all cases where a bean override may potentially override another bean override.Rather, we likely have to track the bean names of all overrides created by
BeanOverrideHandler
s and ensure that no two handlers attempt to override the same bean.Proposal
Detect when two bean override handlers attempt to override the same bean and log a warning.
Related Issues
@MockitoBean
incorrectly injects supertype into subtype field #34025The text was updated successfully, but these errors were encountered: