0
0
Spring Bootframework~10 mins

@WebMvcTest for controller testing in Spring Boot - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to annotate the test class for Spring MVC controller testing.

Spring Boot
@[1](UserController.class)
public class UserControllerTest {
    // test methods
}
Drag options to blanks, or click blank then click option'
ASpringBootTest
BRestController
CDataJpaTest
DWebMvcTest
Attempts:
3 left
💡 Hint
Common Mistakes
Using @SpringBootTest instead of @WebMvcTest for controller tests
Forgetting to specify the controller class in the annotation
2fill in blank
medium

Complete the code to inject the MockMvc object for testing HTTP requests.

Spring Boot
@Autowired
private [1] mockMvc;
Drag options to blanks, or click blank then click option'
ARestTemplate
BWebClient
CMockMvc
DTestRestTemplate
Attempts:
3 left
💡 Hint
Common Mistakes
Using RestTemplate which requires a running server
Not injecting MockMvc and trying to test controllers directly
3fill in blank
hard

Fix the error in the test method to perform a GET request and expect status 200.

Spring Boot
mockMvc.perform(get("/users"))
       .andExpect([1].isOk());
Drag options to blanks, or click blank then click option'
Astatus
Bresult
Crequest
Dresponse
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect method names like result or request instead of status
Not chaining the andExpect call properly
4fill in blank
hard

Fill both blanks to mock a service bean and inject it into the test.

Spring Boot
@[1]
private UserService [2];
Drag options to blanks, or click blank then click option'
AMockBean
BAutowired
CuserService
Dservice
Attempts:
3 left
💡 Hint
Common Mistakes
Using @Autowired instead of @MockBean for mocking
Using generic variable names like service without context
5fill in blank
hard

Fill all three blanks to perform a POST request with JSON content and expect a created status.

Spring Boot
mockMvc.perform(post("/users")
       .contentType([1])
       .content([2]))
       .andExpect(status().[3]());
Drag options to blanks, or click blank then click option'
AMediaType.APPLICATION_JSON
B"{\"name\":\"John\"}"
CisCreated
DMediaType.TEXT_PLAIN
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong media type like TEXT_PLAIN
Not escaping JSON string properly
Expecting wrong status like isOk instead of isCreated