Question 1
Which TWO statements correctly distinguish @MockBean from @SpyBean in a Spring Boot slice test? Select TWO.
A. @MockBean adds or replaces a matching bean with a Mockito mock whose methods you stub with given(...).willReturn(...).Correct answer
@MockBean contributes a Mockito mock, replacing any existing matching bean or adding one, and its behaviour is defined by stubbing rather than by real method calls.
B. @SpyBean wraps the real bean so its real methods execute unless stubbed, which is useful for verifying interactions on the genuine implementation.Correct answer
@SpyBean is a Mockito spy over the actual bean: real methods run by default and can be selectively stubbed, so you can verify interactions against the real object.
C. @SpyBean produces a bean whose every method returns null or a default until you stub it, exactly like a plain mock.
That describes a mock, not a spy; a spy invokes the real methods unless a stub overrides them, so it does not return defaults everywhere by default.
D. Neither @MockBean nor @SpyBean affects the context cache, so the cached context is reused unchanged.
Both annotations mutate the context by inserting a mock or spy, which resets/evicts the cached context, so the claim that the cache is untouched is wrong.
E. @MockBean can only add a brand-new bean and can never replace an existing bean of the same type.
@MockBean will replace an existing matching bean when one is present, not merely add new beans, so this restriction is false.
Explanation
@MockBean installs a Mockito mock — replacing a matching bean or adding one — whose behaviour comes entirely from stubbing, whereas @SpyBean wraps the real bean so its real methods run unless overridden, which is what lets you verify interactions on the genuine implementation. A spy is not a default-returning mock, both annotations reset the context cache because they alter the context, and mock beans can replace existing beans rather than only adding new ones.