Close
« Tutorial

Spring MVC - Unit Testing PATCH Requests MCQ

0/13
0 ✓
0 ✗

1. What is the HTTP PATCH method mainly used for?

2. Which annotation should be placed on the controller method so that it handles HTTP PATCH requests?

@Controller
@RequestMapping("/items")
public class ItemController {

    // annotation here
    @ResponseBody
    public String update(@RequestBody Item item) {
        return "Done";
    }
}

3. What is the main benefit of using mock web objects to test a Spring MVC controller?

4. A test class has an autowired WebApplicationContext named wac. Which statement creates a MockMvcTester from it?

private MockMvcTester tester;

@BeforeEach
public void setup() {
    this.tester = ____;
}

5. Which annotation can be placed on a JUnit 5 test class to load a web configuration class and create a WebApplicationContext for the test?

____
public class ItemControllerTests {

    @Autowired
    private WebApplicationContext wac;
}

6. Which method starts building a mock HTTP PATCH request in MockMvcTester?

tester.____()
      .uri("/items/1")
      .contentType(MediaType.APPLICATION_JSON)
      .content("{\"name\":\"pen\"}")
      .exchange();

7. Which request builder method tells the controller what type of data the request body contains?

tester.patch()
      .uri("/items/1")
      .____(MediaType.APPLICATION_JSON)
      .content("{\"name\":\"pen\"}")
      .exchange();

8. What does the accept() method of a mock request set?

9. Which method sends the built mock request and returns the result to be asserted?

assertThat(tester.patch()
                 .uri("/items/1")
                 .contentType(MediaType.APPLICATION_JSON)
                 .content("{\"name\":\"pen\"}")
                 .____())
        .hasStatusOk();

10. Which assertion checks that the response status is 200 OK?

assertThat(tester.patch().uri("/items/1").exchange())
        .____();

11. A PATCH request sends a JSON body. Which annotation on the parameter binds the body to a Java object?

@PatchMapping("/{id}")
@ResponseBody
public String update(____ Item item) {
    return "Updated";
}

12. Which attribute of @PatchMapping limits a handler method to requests with the form data content type?

@PatchMapping(
        value = "/{id}",
        ____ = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
@ResponseBody
public String update(@RequestBody MultiValueMap params) {
    return "Updated";
}

13. A request has the content type application/x-www-form-urlencoded. Which parameter type with @RequestBody can hold all the form fields?

14. Two controller methods use the same URL and both handle PATCH. When can Spring still choose the correct one?

15. What is the response body text in this test? Assume Item has a name property with a getter and a setter.

@PatchMapping("/{id}")
@ResponseBody
public String update(@RequestBody Item item) {
    return "Updated: " + item.getName();
}

assertThat(tester.patch().uri("/items/1")
        .contentType(MediaType.APPLICATION_JSON)
        .content("{\"name\":\"pen\"}")
        .exchange())
    .bodyText().isEqualTo("?");

16. Why is @EnableWebMvc usually added to the Java configuration used in a Spring MVC controller test?

Join