mardi 30 décembre 2014

Mockito error when mocking Request Dispatcher

I am using "GetRequestDispatcher" to be able to forward attributes between a servlet and a JSP page, like so(the following is taken from the servlet):



request.setAttribute("AttributeValue",message);
request.getRequestDispatcher("DestinationPage.jsp").forward(request, response);


I am now using Mockito to mock the HttpSession as well as the HttpRequest, HttpResponse and the Dispatcher, as follows:



@Test
public void freeRiskAndAmountValidation() throws ServletException,IOException
{
//given
Mockito.doReturn("testAttr").when(session).getAttribute("A");
Mockito.when(request.getRequestDispatcher("DestinationPage.jsp")).thenReturn(dispatcher);
//when
bets.doGet(request,response);
//then
Mockito.verify(dispatcher).forward(request,response);
}


However, my test returns a null pointer exception(console) AND mockito gives me the following error:



Wanted, but not invoked.
dispatcher.forward(request,response);
Actually, there were zero interactions with this mock.


Could anyone tell me what is causing this error, and whether there is a better way of mocking the dispatcher?


Aucun commentaire:

Enregistrer un commentaire