nerdexam
Broadcom-VMware

2V0-72.22PSE · Question #46

Which option is true about use of mocks in a Spring Boot web slice test? (Choose the best answer.)

The correct answer is A. Mocking a Spring Bean requires annotating it with @MockBean annotation. Option A is correct because @MockBean is the Spring Boot-specific annotation that creates a Mockito mock and registers it in the Spring application context, replacing any existing bean of that type. In a @WebMvcTest (web slice test), collaborating beans like services aren't…

Testing

Question

Which option is true about use of mocks in a Spring Boot web slice test? (Choose the best answer.)

Options

  • AMocking a Spring Bean requires annotating it with @MockBean annotation.
  • BIf a Spring Bean already exists in the web slice test spring context, it cannot be mocked.
  • CMocks cannot be used in a Spring Boot web slice test.
  • DMocking a Spring Bean requires annotating it with @Mock annotation.

How the community answered

(24 responses)
  • A
    88% (21)
  • C
    4% (1)
  • D
    8% (2)

Explanation

Option A is correct because @MockBean is the Spring Boot-specific annotation that creates a Mockito mock and registers it in the Spring application context, replacing any existing bean of that type. In a @WebMvcTest (web slice test), collaborating beans like services aren't loaded, so @MockBean is the standard way to provide them to the context.

Why the distractors are wrong:

  • B is the opposite of true - @MockBean can replace an existing bean in the Spring context with a mock, which is precisely its power.
  • C is flatly false - mocking is not only allowed but expected in web slice tests, since the slice intentionally excludes most beans (e.g., services, repositories) that controllers depend on.
  • D confuses @Mock (plain Mockito, no Spring integration) with @MockBean (Spring-aware mock). Using @Mock won't inject the mock into the Spring context, so the controller under test would receive a real or missing bean instead.

Memory tip: Think of the "B" in @MockBean as standing for "Bean" - it's the only annotation that puts the mock into the Spring Bean context. If you're in a Spring test, you need the Spring-aware version.

Topics

#Web Slice Testing#@MockBean#Spring Beans#Mocking

Community Discussion

No community discussion yet for this question.

Full 2V0-72.22PSE Practice