gpt4 book ai didi

java - 测试 HttpServletResponse addCookie

转载 作者:行者123 更新时间:2023-12-02 06:32:50 24 4
gpt4 key购买 nike

我想用 JUnit 和 Mockachino 测试这个方法。

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

但是如何呢?

最佳答案

我首选的模拟框架是Mockito。但你可以用 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());
}

关于java - 测试 HttpServletResponse addCookie,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19925467/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com