title: How can i use Mockito to test forward only?
body: <p>The Controller use request.getParameter to forward, the code like below</p>
  
  <pre><code>public class ComputingController extends HttpServlet {
  
      public ComputingController() {
          super();
      }
  
      public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  
          String reDirect = request.getParameter("redirect");
          RequestDispatcher rd = null;
  
          if ( "update".equals( reDirect ) ) {
  
              UpdateData updateData = new UpdateData();
              String updateResult = updateData.doUpdateData();
              request.setAttribute( "updateResult", updateResult );
  
              rd = request.getRequestDispatcher("WEB-INF/jsp/computing/success.jsp");
              rd.forward(request, response);
          }
          else {
  
              rd = request.getRequestDispatcher("WEB-INF/jsp/computing/error.jsp");
              rd.forward(request, response);
          }
      }
  
  }
  </code></pre>
  
  <p>I use Junit and Mockito to test forward. </p>
  
  <p>But when i execute the test (like below), it will both execute updateData.doUpdateData() in the controller.</p>
  
  <p>How can i use the Mockito let it don't execute updateData.doUpdateData() but forward ?</p>
  
  <pre><code>public class ComputingControllerTest {
  
      @Mock
      HttpServletRequest request;
  
      @Mock
      HttpServletResponse response;
  
      @Mock
      RequestDispatcher rd;
  
      @Before
      public void setUp() throws Exception {
          MockitoAnnotations.initMocks(this);
      }
  
      public void doGet_testForward() throws Exception {
  
          String reDirect = "update";
  
          when(request.getParameter("redirect")).thenReturn( reDirect );
  
          when(request.getRequestDispatcher("WEB-INF/jsp/computing/success.jsp")).thenReturn(rd);
  
          new ComputingController().doGet(request, response);
  
          verify(rd).forward(request, response);
  
      }
  }
  </code></pre>