heffaklump
heffaklump

Reputation: 1606

Testing HttpServletResponse addCookie

I have this method I want to test with JUnit and Mockachino.

public void removeCookie(HttpServletResponse response, String name) {
    Cookie cookie = new Cookie(name, "");
    cookie.setMaxAge(0);
    response.addCookie(cookie);
}

But how?

Upvotes: 0

Views: 3255

Answers (3)

Todd
Todd

Reputation: 642

Wow. Ancient topic I've landed on here. For what it's worth I've found the MockHttpServletRequest and MockHttpServletResponse classes from Spring to be VERY helpful for this sort of stuff. They can be used even if your project is NOT a Spring project. Only one dependency is needed:

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-test</artifactId>
    <version>5.3.20</version>
    <scope>test</scope>
</dependency>

Then I do stuff like this:

    MockHttpServletRequest request = new MockHttpServletRequest();
    MockHttpServletResponse response = new MockHttpServletResponse();

    methodThatTakesRequestOrResponse(request, response);

    // Validate state (cookies, attributes, etc.)
    String attribute = (String)request.getAttribute("expected attribute name");
    assertNotNull(attribute);
    String cookie = response.getCookie("expected cookie name").getValue();
    assertNotNull(cookie);

Upvotes: 0

Ashwin Krishnamurthy
Ashwin Krishnamurthy

Reputation: 3758

A late answer for posterity's sake. Here is a snippet I used to test the addCookie functionality using EasyMock and the awesomeness that is Capture:

Capture<Cookie> getCookie = new Capture<Cookie>();

//mocking
HttpServletRequest request = new MockHttpServletRequest();
Model model = new ExtendedModelMap();
HttpServletResponse response = createNiceMock(HttpServletResponse.class);
MyController controller = createMock(MyController.class);


// expectation
response.addCookie( capture(getCookie) ); //Capture would look for a Cookie object being sent to the addCookie method and grab that for you

// invocation
replay(controller, response);
String result = controller.someAction(request, response, model);
verify(controller, response);

// assertions
Cookie cookie = getCookie.getValue();
assertEquals("myCookie", cookie.getName());

Good luck.

Upvotes: 1

Eugen Martynov
Eugen Martynov

Reputation: 20140

My preferred mocking framework is Mockito. But you can try this with Mockachino:

@Test
public void providedNameAndEmptyValueCookieSetToResponse() {
   String name = "name";
   HttpServletResponse responseMock = mock(HttpServletResponse.class, RETURNS_DEEP_STUBS);

   removeCookie(responseMock, name);

   Cookie cookie = catchCookie(mock);
   assertEquals(name, cookie.getName());
   assertEquals("", cookie.getValue());
}

private Cookie catchCookie() {
   ArgumentCatcher<Cookie> catcher = ArgumentCatcher.create(new AnyMatcher(Cookie.class));
   verifyOnce().on(responseMock).addCookie(match(catcher));

  return catcher.getValue();
}

@Test
public void zeroMaxAgeCookieSetToResponse() {
   HttpServletResponse responseMock = mock(HttpServletResponse.class, RETURNS_DEEP_STUBS);

   removeCookie(responseMock, "any");

   Cookie cookie = catchCookie(mock);
   assertEquals(Integer.valueOf(0), cookie.getMaxAge());
}

Upvotes: 1

Related Questions