Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MockMvc configure a header for all requests

In my tests I setup the MockMvc object in the @Before like this

mockMvc = MockMvcBuilders.webAppContextSetup(context)
                .apply(springSecurity())
                .build();

In every request I do I always need to send the same headers. Is there a way to configure the headers the MockMvc will use globally or per test class?

like image 937
isADon Avatar asked May 25 '18 15:05

isADon


People also ask

What does the Accept header do in mockmvc?

Finally, an accept header is set to tell the endpoint the client expects a JSON response. Lines 10 to 15 use statically imported methods from MockMvcResukltMatchers to perform assertions on the response. We begin by checking that the response code returned is 201 'Created' and that the content type is JSON.

How does mockmvc handle HTTP requests?

The class will handle the HTTP request and pass it further to our controller. Controller code will be executed in exactly the same way as if it was processing the real request, just without the cost of starting a web server. MockMvc also carries several useful methods for performing requests and asserting on results.

What is mockmvc in Spring Boot?

Spring boot MockMVC example Learn to use Spring MockMVC to perform integration testing of Spring webmvc controllers. MockMVC class is part of Spring MVC test framework which helps in testing the controllers explicitly starting a Servlet container.

What is the process for using mockmvc?

The process for using MockMVC is setting up a MockMVC object: . . . } [/sourcecode]Once we have the MockMVC object, we can start building a request and then test expectations on the response. } [/sourcecode]This simple test checks that a GET request to /index.do returns a 2xx response.


2 Answers

You can write an implementation of javax.servlet.Filter. In your case, you can add the headers into your request. MockMvcBuilders has a method to add filters:

mockMvc = MockMvcBuilders.webAppContextSetup(context)
            .apply(springSecurity())
            .addFilter(new CustomFilter(), "/*")
            .build();
like image 176
borino Avatar answered Sep 19 '22 09:09

borino


How about you make a factory class to start you off with your already decrorated-with-headers request? Since MockHttpServletRequestBuilder is a builder, you just decorate the request with any of the additional properties (params, content type, etc.) that you need. The builder is designed just for this purpose! For example:

public class MyTestRequestFactory {

    public static MockHttpServletRequestBuilder myFactoryRequest(String url) {
        return MockMvcRequestBuilders.get(url)
                .header("myKey", "myValue")
                .header("myKey2", "myValue2");
    }
}

Then in your test:

@Test
public void whenITestUrlWithFactoryRequest_thenStatusIsOK() throws Exception {

    mockMvc()
        .perform(MyTestRequestFactory.myFactoryRequest("/my/test/url"))
        .andExpect(status().isOk());
}

@Test
public void whenITestAnotherUrlWithFactoryRequest_thenStatusIsOK() throws Exception {

    mockMvc()
        .perform(MyTestRequestFactory.myFactoryRequest("/my/test/other/url"))
        .andExpect(status().isOk());
}

Each test will call the endpoint with the same headers.

like image 22
Dovmo Avatar answered Sep 18 '22 09:09

Dovmo