Bird
0
0

What is the effect of using @MockBean on a Spring component in a test context?

easy📝 Conceptual Q1 of 15
Spring Boot - Testing Spring Boot Applications
What is the effect of using @MockBean on a Spring component in a test context?
AIt replaces the real bean with a Mockito mock in the application context during the test
BIt creates a new instance of the real bean without any mocking
CIt disables the bean from being loaded in the test context
DIt automatically injects dependencies into the bean without mocking
Step-by-Step Solution
Solution:
  1. Step 1: Understand @MockBean behavior

    @MockBean creates a Mockito mock and replaces the existing bean in the Spring test context.
  2. Step 2: Effect on application context

    The mock bean is injected wherever the original bean was used, allowing controlled behavior in tests.
  3. Final Answer:

    It replaces the real bean with a Mockito mock in the application context during the test -> Option A
  4. Quick Check:

    MockBean replaces real beans with mocks [OK]
Quick Trick: MockBean replaces real beans with mocks in tests [OK]
Common Mistakes:
  • Confusing @MockBean with @Mock which does not replace Spring beans
  • Assuming @MockBean disables the bean instead of mocking it

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Spring Boot Quizzes