Mitaksh Gupta
Mitaksh Gupta

Reputation: 1029

Adding cookie to HttpServletRequest and retrieving cookie from HttpServletResponse using Mockito

I am using Mockito to mock HttpServletRequest and HttpServletResponse. I want to add cookie in the the mock request I am creating. How can I do so?

Also I am setting the cookie in the response at the server side. How can I retrieve the cookie from the mock response sent by the server?

Upvotes: 3

Views: 12849

Answers (5)

beheshtraya
beheshtraya

Reputation: 61

import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;

class ComponentTests {

    private HttpServletRequest testRequest = Mockito.mock(HttpServletRequest.class);

    @Test
    void testMethod() {
        Cookie[] testCookies = new Cookie[]{new Cookie("name", "value")};
        Mockito.when(testRequest.getCookies()).thenReturn(testCookies);

        // Assertions
    }

}

Upvotes: 3

Milan Paudyal
Milan Paudyal

Reputation: 561

You can try something like this.

import javax.servlet.http.Cookie;

@RunWith(MockitoJUnitRunner.class)
public class TestClass {

    private MockHttpServletRequest servletRequest;

    @Before
    public void setUp() {
        servletRequest = new MockHttpServletRequest();
        Cookie[] cookies = new Cookie[]{
            new Cookie("test_key", "test_value")
        };
        servletRequest.setCookies(cookies);
    }

    @Test
    public void methodTest() {
        Assert.assertEquals("test_value", servletRequest.getCookies() 
        [0].getValue());
    }

}

Upvotes: 5

ha9u63a7
ha9u63a7

Reputation: 6824

The best way to do things is to use Spring's MockHttpServletRequest and MockHttpServletResponse.

They are wrapper implementation with getters for cookie. These are exactly what you need to ensure that cookie setup can be verified. Mockito isn't sufficient here.

Upvotes: 0

Hari Rao
Hari Rao

Reputation: 3240

Use MockHttpServletResponse that implements HttpServletResponse. It has getCookies / getCookie method on which assertions can be done.

Upvotes: 1

Jonathan
Jonathan

Reputation: 20375

For the request: construct the array, adding any Cookies you want, then add the behaviour to the mock:

final Cookies[] cookies = new Cookies[] { ... };

final HttpServletRequest request = mock(HttpServletRequest.class);
given(request.getCookies()).thenReturn(cookies);

... pass to controller/servlet etc ...

For the response you create the mock and then verify the addCookie call by either using an ArgumentCaptor to capture the actual cookie passed to addCookie:

final ArgumentCapor<Cookie> captor = ArgumentCaptor.forClass(Cookie.class);

verify(response).addCookie(captor.capture());

final List<Cookie> cookies = captor.getValue();

... perform asserion on cookies ...

Or build the expected cookie and verify:

final Cookie expectedCookie = ...

verify(response).addCookie(expectedCookie);

Personally I prefer not using an ArgumentCaptor but it largely depends on your test.

Upvotes: 1

Related Questions