Breaking News: Grepper is joining You.com. Read the official announcement!
Check it out

MockMvc Test Setup

Pragya Keshap answered on February 24, 2023 Popularity 5/10 Helpfulness 4/10

Contents


More Related Answers

  • Mock MVC Test with Spring Boot
  • Invoke and Test an API Endpoint with MockMvc
  • What is MockMvc?
  • springboottest vs mockmvc test
  • Unit Testing With @WebMvcTest
  • To test the Controllers, we can use @WebMvcTest
  • Testing web controllers with MockMVC
  • How do you write a unit test with MockMVC?

  • MockMvc Test Setup

    0

    There are two ways to create a MockMvc instance: using Spring Boot's auto-configuration or hand-crafting it.

    Following Spring Boot's auto-configuration principle, we only need to annotate our test with @WebMvcTest. This annotation not only ensures to auto-configure MockMvc but also creates a sliced Spring context containing only MVC-related beans.

    To keep the sliced test context small, we can pass the class name of the controller we want to test: @WebMvcTest(MyController.cass). Otherwise, Spring will create a context including all our controller endpoints.

    The second approach is helpful if we're using Spring without Spring Boot or if we want to fine-tune the setup. The MockMvcBuilders class provides several entry points to construct a MockMvc instance: 

    Popularity 5/10 Helpfulness 4/10 Language java
    Source: Grepper
    Tags: java mockmvc
    Link to this answer
    Share Copy Link
    Contributed on Feb 24 2023
    Pragya Keshap
    0 Answers  Avg Quality 2/10


    X

    Continue with Google

    By continuing, I agree that I have read and agree to Greppers's Terms of Service and Privacy Policy.
    X
    Grepper Account Login Required

    Oops, You will need to install Grepper and log-in to perform this action.