0
0
JUnittesting~10 mins

@WebMvcTest for controller testing in JUnit - 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 controller testing with Spring Boot.

JUnit
@[1](MyController.class)
public class MyControllerTest {
    // test methods
}
Drag options to blanks, or click blank then click option'
AWebMvcTest
BSpringBootTest
CDataJpaTest
DRestController
Attempts:
3 left
💡 Hint
Common Mistakes
Using @SpringBootTest instead of @WebMvcTest for controller tests.
Annotating with @RestController which is for controllers, not tests.
2fill in blank
medium

Complete the code to inject the MockMvc object for performing HTTP requests in the test.

JUnit
@Autowired
private [1] mockMvc;
Drag options to blanks, or click blank then click option'
AWebClient
BRestTemplate
CMockMvc
DTestRestTemplate
Attempts:
3 left
💡 Hint
Common Mistakes
Using RestTemplate which is for real HTTP calls, not mocks.
Using WebClient which is reactive and not typical in @WebMvcTest.
3fill in blank
hard

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

JUnit
mockMvc.perform(get("/api/items"))
       .andExpect([1].status().isOk());
Drag options to blanks, or click blank then click option'
AMockMvcRequestBuilders
BMvcResult
CResultActions
DMockMvcResultMatchers
Attempts:
3 left
💡 Hint
Common Mistakes
Using MockMvcRequestBuilders which builds requests, not assertions.
Using MvcResult which holds the result, not matchers.
4fill in blank
hard

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

JUnit
@[1]
private MyService myService;
Drag options to blanks, or click blank then click option'
AMockBean
BSpyBean
CInjectMocks
DAutowired
Attempts:
3 left
💡 Hint
Common Mistakes
Using @Autowired to inject real beans instead of mocks.
Using @InjectMocks which is from Mockito but not for Spring context.
5fill in blank
hard

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

JUnit
mockMvc.perform(post("/api/items")
       .contentType(MediaType.[1])
       .content("{\"name\":\"item1\"}"))
       .andExpect(content().contentType(MediaType.[2]))
       .andExpect(status().[3]());
Drag options to blanks, or click blank then click option'
AAPPLICATION_JSON
BAPPLICATION_XML
CisCreated
DisOk
Attempts:
3 left
💡 Hint
Common Mistakes
Using APPLICATION_XML instead of APPLICATION_JSON for JSON data.
Expecting isCreated() when the controller returns 200 OK.