Bird
0
0

You want to write an integration test that loads the full Spring context but mocks only one bean. How can you achieve this with @SpringBootTest?

hard📝 Application Q8 of 15
Spring Boot - Testing Spring Boot Applications
You want to write an integration test that loads the full Spring context but mocks only one bean. How can you achieve this with @SpringBootTest?
AUse @SpringBootTest with @MockBean on the bean to mock.
BUse @SpringBootTest and manually create a mock inside the test method.
CUse @SpringBootTest with @Autowired on the mock bean.
DUse @SpringBootTest(webEnvironment = MOCK) to mock all beans.
Step-by-Step Solution
Solution:
  1. Step 1: Understand how to mock beans in integration tests

    @MockBean annotation allows replacing a bean in the Spring context with a mock while loading the full context.
  2. Step 2: Evaluate options

    Use @SpringBootTest with @MockBean on the bean to mock. correctly combines @SpringBootTest with @MockBean. Use @SpringBootTest and manually create a mock inside the test method. creates a local mock but does not replace the Spring bean. Use @SpringBootTest with @Autowired on the mock bean. misuses @Autowired. Use @SpringBootTest(webEnvironment = MOCK) to mock all beans. is invalid.
  3. Final Answer:

    Use @SpringBootTest with @MockBean on the bean to mock. -> Option A
  4. Quick Check:

    @MockBean replaces beans in @SpringBootTest context [OK]
Quick Trick: Use @MockBean to mock specific beans in @SpringBootTest [OK]
Common Mistakes:
  • Creating mocks without @MockBean annotation
  • Misusing @Autowired for mocks
  • Assuming webEnvironment=MOCK mocks beans

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Spring Boot Quizzes