Bird
0
0

Consider a test with two @MockBean declarations for the same bean type InventoryService. What happens when the test runs?

hard📝 Conceptual Q9 of 15
Spring Boot - Testing Spring Boot Applications
Consider a test with two @MockBean declarations for the same bean type InventoryService. What happens when the test runs?
ABoth mocks are injected into different places automatically
BThe last declared @MockBean replaces the previous one in the context
CSpring throws a bean definition conflict error at startup
DThe test runs but only the first mock is used
Step-by-Step Solution
Solution:
  1. Step 1: Understand @MockBean behavior with duplicates

    When multiple @MockBean of the same type exist, the last one overrides the previous in the context.
  2. Step 2: Confirm Spring behavior

    Spring does not throw an error but uses the last declared mock for injection.
  3. Final Answer:

    The last declared @MockBean replaces the previous one in the context -> Option B
  4. Quick Check:

    Last @MockBean wins for same bean type [OK]
Quick Trick: Only the last @MockBean of a type is active in the context [OK]
Common Mistakes:
  • Expecting errors on duplicate @MockBean
  • Assuming multiple mocks coexist for same bean type
  • Confusing @MockBean with @Mock behavior

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Spring Boot Quizzes