0
0
JUnittesting~20 mins

@WebMvcTest for controller testing in JUnit - 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!
Predict Output
intermediate
2:00remaining
Output of a simple @WebMvcTest controller test
Given the following Spring Boot controller and test, what will be the result of the test execution?
JUnit
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.junit.jupiter.api.Test;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@WebMvcTest(HelloController.class)
class HelloControllerTest {

    @Autowired
    private MockMvc mockMvc;

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

@RestController
class HelloController {
    @GetMapping("/hello")
    public String hello() {
        return "Hello World";
    }
}
ATest passes because the /hello endpoint returns HTTP 200 OK
BTest fails with compilation error due to missing @SpringBootTest annotation
CTest fails with 500 Internal Server Error due to missing service bean
DTest fails with 404 Not Found because the controller is not loaded
Attempts:
2 left
💡 Hint
Remember that @WebMvcTest loads only the controller layer and mocks other beans.
assertion
intermediate
2:00remaining
Correct assertion to verify JSON response in @WebMvcTest
You have a controller endpoint that returns JSON: {"message":"success"}. Which assertion correctly verifies this JSON response in a @WebMvcTest?
JUnit
mockMvc.perform(get("/api/status"))
A.andExpect(content().string("{message:success}"))
B.andExpect(content().json("{\"message\":\"success\"}"))
C.andExpect(jsonPath("$.message").value("success"))
D.andExpect(status().isOk()).andExpect(content().contentType("text/plain"))
Attempts:
2 left
💡 Hint
Use jsonPath to check JSON fields precisely.
🔧 Debug
advanced
2:00remaining
Identify cause of 404 error in @WebMvcTest
A @WebMvcTest for a controller returns 404 Not Found when testing the endpoint. Which is the most likely cause?
AThe MockMvc bean is not autowired correctly
BThe controller class is not included in the @WebMvcTest annotation
CThe test method is missing @Test annotation
DThe controller method returns null instead of a response
Attempts:
2 left
💡 Hint
Check which controllers @WebMvcTest loads.
framework
advanced
2:00remaining
Behavior of @WebMvcTest with service layer dependencies
When using @WebMvcTest on a controller that depends on a service bean, what happens if the service bean is not mocked or provided?
ATest passes but service methods are not called
BTest passes because @WebMvcTest automatically mocks all dependencies
CTest fails with NullPointerException inside the controller
DTest fails with NoSuchBeanDefinitionException for the service bean
Attempts:
2 left
💡 Hint
Consider what beans @WebMvcTest loads by default.
🧠 Conceptual
expert
2:00remaining
Purpose and scope of @WebMvcTest in Spring Boot testing
Which statement best describes the purpose and scope of @WebMvcTest in Spring Boot testing?
A@WebMvcTest loads only the web layer components such as controllers, filters, and related configurations for focused controller testing
B@WebMvcTest is used to test repository layer with mock database connections
C@WebMvcTest loads the full application context including database and service layers for integration testing
D@WebMvcTest automatically scans and loads all beans annotated with @Component in the application
Attempts:
2 left
💡 Hint
Think about which parts of the app @WebMvcTest focuses on.