0
0
Spring Bootframework~20 mins

@WebMvcTest for controller testing in Spring Boot - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
WebMvcTest Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What does @WebMvcTest load in the Spring context?
When you use @WebMvcTest on a controller class, which parts of the Spring application context are loaded for the test?
ANo Spring context is loaded; it runs the controller in isolation without Spring support.
BOnly the web layer components like controllers, filters, and related MVC components are loaded.
COnly the repository layer is loaded to test database interactions.
DThe entire application context including services, repositories, and controllers is loaded.
Attempts:
2 left
💡 Hint
Think about what is needed to test just the web layer without starting the full app.
state_output
intermediate
2:00remaining
What is the HTTP status returned by this @WebMvcTest controller test?
Given this controller and test, what HTTP status code will the test receive?
Spring Boot
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.junit.jupiter.api.Test;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

@WebMvcTest(HelloController.class)
class HelloControllerTest {
    @Autowired
    private MockMvc mockMvc;

    @Test
    void testHello() throws Exception {
        mockMvc.perform(get("/hello"))
               .andExpect(status().isOk());
    }
}

@RestController
class HelloController {
    @GetMapping("/hello")
    public String sayHello() {
        return "Hello!";
    }
}
A404 Not Found
B500 Internal Server Error
C200 OK
D302 Found (Redirect)
Attempts:
2 left
💡 Hint
The controller has a GET mapping for /hello that returns a string.
📝 Syntax
advanced
2:00remaining
Which @WebMvcTest annotation usage is correct for testing multiple controllers?
You want to test two controllers, UserController and OrderController, using @WebMvcTest. Which annotation usage is correct?
A@WebMvcTest({UserController.class, OrderController.class})
B@WebMvcTest(UserController.class, OrderController.class)
C@WebMvcTest(controllers = UserController.class, OrderController.class)
D@WebMvcTest(controllers = {UserController.class, OrderController.class})
Attempts:
2 left
💡 Hint
Check the syntax for specifying multiple controllers in the annotation attribute.
🔧 Debug
advanced
2:00remaining
Why does this @WebMvcTest fail with NoSuchBeanDefinitionException?
This test fails with NoSuchBeanDefinitionException for UserService. Why?
Spring Boot
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.junit.jupiter.api.Test;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

@WebMvcTest(UserController.class)
class UserControllerTest {
    @Autowired
    private MockMvc mockMvc;

    @Test
    void testUser() throws Exception {
        mockMvc.perform(get("/user"))
               .andExpect(status().isOk());
    }
}

@RestController
class UserController {
    private final UserService userService;

    public UserController(UserService userService) {
        this.userService = userService;
    }

    @GetMapping("/user")
    public String getUser() {
        return userService.getName();
    }
}

interface UserService {
    String getName();
}
AThe UserService bean is not found because @WebMvcTest does not load service beans unless mocked.
BUserService is an interface and cannot be mocked with @MockBean.
CThe test forgot to add @MockBean for UserService, so Spring cannot inject it.
DThe controller constructor is missing @Autowired, so injection fails.
Attempts:
2 left
💡 Hint
Think about what beans @WebMvcTest loads and how to provide dependencies.
🧠 Conceptual
expert
3:00remaining
What is the main advantage of using @WebMvcTest over @SpringBootTest for controller tests?
Why would a developer choose @WebMvcTest instead of @SpringBootTest when testing controllers?
A@WebMvcTest loads only the web layer, making tests faster and more focused on controller logic.
B@WebMvcTest automatically mocks all dependencies, so no manual mocking is needed.
C@WebMvcTest runs the full application context including database connections for integration testing.
D@WebMvcTest disables Spring Security, so tests do not require authentication setup.
Attempts:
2 left
💡 Hint
Consider test speed and scope of loaded beans.