title: Mockito TooManyActualInvocations
body: <p>I'm using Junit and Mockito to test forward.
  Here is part of <code>PortalServletTest</code> class:</p>
  
  <pre><code>@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&lt;List&lt;String&gt;&gt; mandatoryHeaders = new ArrayList&lt;List&lt;String&gt;&gt;();
      mandatoryHeaders.add(new ArrayList&lt;String&gt;() {
          {
              add("HTTP_XXXX");
              add("http-xxxx");
          }
      });
  
      List&lt;List&lt;String&gt;&gt; optionalHeaders = new ArrayList&lt;List&lt;String&gt;&gt;();
      optionalHeaders.add(new ArrayList&lt;String&gt;() {
          {
              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&lt;Object&gt;() {
          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));
  }
  </code></pre>
  
  <p>The <code>PortalServlet</code> code:</p>
  
  <pre><code>RequestDispatcher rd = request.getRequestDispatcher("index.html");
          rd.forward(mutableRequest, response);
  </code></pre>
  
  <p>The problem is that when testing the class, I'm getting the error message:</p>
  
  <blockquote>
    <p>requestDispatcher.forward(, );
    Wanted 1 time:
    -> at xxx.PortalServletTest.testSuccesfulRequest(PortalServletTest.java:140)</p>
    
    <p>But was 2 times. Undesired invocation:
    -> at xxx.PortalServlet.addRequestHeaders(PortalServlet.java:144)</p>
    
    <p>at xxx.PortalServletTest.testSuccesfulRequest(PortalServletTest.java:140)</p>
  </blockquote>
  
  <p>If I run each test individually they pass OK.
  Looks like the forward from <code>PortalServlet</code> is count twice for each test.
  Any suggestion how to solve this issue?</p>
  
  <p>Thanks in advance.</p>