Mockito,如何在方法中创建的对象上调用验证?
Mockito, how can I call verify on an object created in a method?
简单示例:
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
HttpServletRequest req = (HttpServletRequest) request;
HttpServletResponse res = (HttpServletResponse) response;
res.addCookie(cookie);
chain.doFilter(request, response);
}
如果 HttpServletResponse 对象是从 ServletResponse 创建的,我如何验证是否调用了 addCookie 方法。
将 HttpServletResponse
的 mock 传递给过滤器,然后对其进行验证。
我们需要测试的自定义过滤器
import javax.servlet.*;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
public class CustomFilter implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
}
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain) throws IOException, ServletException {
HttpServletRequest req = (HttpServletRequest) request;
HttpServletResponse res = (HttpServletResponse) response;
res.addCookie(new Cookie("name", "value")); // filter call addCookie method which need to be verified
filterChain.doFilter(request, response);
}
@Override
public void destroy() {
}
}
测试实现
import org.junit.jupiter.api.Test;
import javax.servlet.FilterChain;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import static org.mockito.Mockito.*;
public class FilterTest {
CustomFilter customFilter = new CustomFilter();
@Test
public void testDoFilter() throws Exception {
HttpServletRequest request = mock(HttpServletRequest.class);
//create mock of HttpServletResponse object
HttpServletResponse response = mock(HttpServletResponse.class);
FilterChain filterChain = mock(FilterChain.class);
//pass HttpServletResponse object to the filter
customFilter.doFilter(request, response, filterChain);
//verify that addCookie method was executed with correct value
verify(response, times(1)).addCookie(argThat(cookie -> "name".equals(cookie.getName()) && "value".equals(cookie.getValue())));
}
}
简单示例:
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
HttpServletRequest req = (HttpServletRequest) request;
HttpServletResponse res = (HttpServletResponse) response;
res.addCookie(cookie);
chain.doFilter(request, response);
}
如果 HttpServletResponse 对象是从 ServletResponse 创建的,我如何验证是否调用了 addCookie 方法。
将 HttpServletResponse
的 mock 传递给过滤器,然后对其进行验证。
我们需要测试的自定义过滤器
import javax.servlet.*;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
public class CustomFilter implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
}
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain) throws IOException, ServletException {
HttpServletRequest req = (HttpServletRequest) request;
HttpServletResponse res = (HttpServletResponse) response;
res.addCookie(new Cookie("name", "value")); // filter call addCookie method which need to be verified
filterChain.doFilter(request, response);
}
@Override
public void destroy() {
}
}
测试实现
import org.junit.jupiter.api.Test;
import javax.servlet.FilterChain;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import static org.mockito.Mockito.*;
public class FilterTest {
CustomFilter customFilter = new CustomFilter();
@Test
public void testDoFilter() throws Exception {
HttpServletRequest request = mock(HttpServletRequest.class);
//create mock of HttpServletResponse object
HttpServletResponse response = mock(HttpServletResponse.class);
FilterChain filterChain = mock(FilterChain.class);
//pass HttpServletResponse object to the filter
customFilter.doFilter(request, response, filterChain);
//verify that addCookie method was executed with correct value
verify(response, times(1)).addCookie(argThat(cookie -> "name".equals(cookie.getName()) && "value".equals(cookie.getValue())));
}
}