Mockito 太多实际调用

Isr*_*ael 6 java junit unit-testing mockito

我正在使用 Junit 和 Mockito 进行测试。这是PortalServletTest课程的一部分:

@SuppressWarnings("serial")
@BeforeClass
public static void setUpTests() {
    when(request.getRequestDispatcher(Mockito.anyString())).thenReturn(rd);
    when(request.getSession()).thenReturn(httpSession);
    when(httpSession.getServletContext()).thenReturn(servletContext);
    when(servletContext.getAttribute(Constants.CONFIGURATION_MANAGER_ATTR)).thenReturn(configurationManager);
    when(configurationManager.getConfiguration()).thenReturn(configuration);
    List<List<String>> mandatoryHeaders = new ArrayList<List<String>>();
    mandatoryHeaders.add(new ArrayList<String>() {
        {
            add("HTTP_XXXX");
            add("http-xxxx");
        }
    });

    List<List<String>> optionalHeaders = new ArrayList<List<String>>();
    optionalHeaders.add(new ArrayList<String>() {
        {
            add("HTTP_YYYY");
            add("http-yyyy");
        }
    });

    when(configuration.getIdentificationHeaderFields()).thenReturn(mandatoryHeaders);
    when(configuration.getOptionalHeaderFields()).thenReturn(optionalHeaders);

}

@Test
public void testMissingHeadersRequest() throws IOException {
    when(request.getHeader(Mockito.anyString())).thenReturn(null);
    target().path("/portal").request().get();
    Mockito.verify(response, times(1)).sendError(HttpServletResponse.SC_USE_PROXY, PortalServlet.MISSING_HEADERS_MSG);
}

@Test
public void testSuccesfulRequest() throws IOException, ServletException {
    Mockito.doAnswer(new Answer<Object>() {
        public Object answer(InvocationOnMock invocation) {
            Object[] args = invocation.getArguments();
            String headerName = (String) args[0];
            return headerName;
        }
    }).when(request).getHeader(Mockito.anyString());
    target().path("/portal").request().get();
    verify(rd).forward(Mockito.any(ServletRequest.class), Mockito.any(ServletResponse.class));
}
Run Code Online (Sandbox Code Playgroud)

代码PortalServlet

RequestDispatcher rd = request.getRequestDispatcher("index.html");
        rd.forward(mutableRequest, response);
Run Code Online (Sandbox Code Playgroud)

问题是,在测试该类时,我收到错误消息:

requestDispatcher.forward(, ); 想要 1 次: -> 在 xxx.PortalServletTest.testSuccesfulRequest(PortalServletTest.java:140)

但已经是2次了。不需要的调用:-> 在 xxx.PortalServlet.addRequestHeaders(PortalServlet.java:144)

在 xxx.PortalServletTest.testSuccesfulRequest(PortalServletTest.java:140)

如果我单独运行每个测试,它们就会通过。PortalServlet看起来每次测试的转发都会计数两次。有什么建议如何解决这个问题吗?

提前致谢。

Gho*_*ica 5

您正在使用 @BeforeClass 来配置模拟对象。

在执行测试类中的 @Tests 之前,该方法会被调用一次。

您可以尝试将其更改为@Before!

换句话说:在进行任何测试之前,您将模拟配置为允许一次调用。但随后您正在运行多个测试。如果您假设您的模拟都以相同的方式使用,则只需每次为每个 @Test 方法重新配置它们即可。

鉴于您的评论:这样做吗

verify(rd, times(2)).forward ...
Run Code Online (Sandbox Code Playgroud)

工作/帮助?


Eni*_*igo 5

除了 @GhostCat 所写的之外,我认为您应该在测试之前重置所有模拟对象:

@Before
public void before() {
   Mockito.reset(/*mocked objects to reset*/)
   // mock them here or in individual tests
}
Run Code Online (Sandbox Code Playgroud)