How to test cookie values using Spring mockMvc

For a given method in your controller:

  @RequestMapping(value = “/sampleSite/home”, method = RequestMethod.GET)

    public String getHomePage(HttpServletRequest request, Model model,

            @Configuration ConfigurationEntity configEntity,

            @CookieValue(value = “iIes”, required = false) String someCookie)

            throws ErrorException {

Some Code

}

You can test the @CookieValue parameter using Spring’s MVC Test Framework like so:

     @Test

    public void test_invalidPaymentSessionCookie() throws Exception {

        // Put the cookie in the request

        final String cookieValue = “some value”;

        final Cookie someInformationCookie = new Cookie(“iIes”, someCookie);

 

        MvcResult result = mockMvc

                .perform(get(“/sampleSite/home”)

                .cookie(invalidSessionCookie)

                .param(“configEntity”, “configEntity”))

                .andExpect(status().isOk())

                .andReturn();     

        assertThat(<getCookieFromRequest>, is(cookieValue));

    }

Advertisements
How to test cookie values using Spring mockMvc

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s