What frameworks are used for testing Spring MVC components?

Testing Spring MVC components typically combines a few complementary tools: JUnit as the test runner, Mockito for isolating dependencies, and Spring's own MockMvc for simulating HTTP requests against controllers. Together they cover everything from a single method to a full request-response cycle.

Key Points: • JUnit (usually JUnit 5) provides the test lifecycle, assertions, and annotations that structure and run the tests. • Mockito creates mock implementations of services and repositories so a controller can be tested in isolation from real dependencies. • Spring Test's MockMvc simulates HTTP requests against controllers without starting a real servlet container, letting you assert on status codes, headers, and response bodies. • @WebMvcTest configures a focused Spring context containing just the web layer, wiring MockMvc automatically for controller-focused tests. • AssertJ is commonly used alongside JUnit for more expressive, fluent assertions than the built-in JUnit assertion methods.

Example: A controller test might use @WebMvcTest to load only the web layer, @MockBean to stub the service layer with Mockito, and MockMvc to perform a simulated GET request and assert the JSON response with jsonPath.

Interview Tip: A concise interview answer is:

"For Spring MVC testing I rely on JUnit 5 for structure, Mockito to mock out services and repositories, and Spring's MockMvc, usually via @WebMvcTest, to simulate HTTP requests against controllers without a real server. That combination covers routing, status codes, and serialization quickly and in isolation from the rest of the app."