Luis
Luis

Reputation: 75

how to know where HttpServletResponse is redirecting?

I'm taking Software Testing because I'm majoring in CS. The professor gave us the source code of a program made in Java to test it. I'm testing right now this method:

    public static void createPanel(HttpServletRequest req, HttpServletResponse res, HttpSession hs) throws IOException
{
    String panelName = req.getParameter("panelName");
    String panelDescription = req.getParameter("panelDescription");
    int employeeID = ((EmployeeProfile)hs.getAttribute("User Profile")).EmployeeID;
    boolean result;

    //Let's validate our fields
    if(panelName.equals("") || panelDescription.equals(""))
        result =  false;
    else
        result = DBManager.createPanel(panelName, panelDescription, employeeID);
    b = result;

    //We'll now display a message indicating the success of the operation to the user
    if(result)
        res.sendRedirect("messagePage?messageCode=Panel has been successfully created.");
    else
        res.sendRedirect("errorPage?errorCode=There was an error creating the panel. Please try again.");

}

I'm using Eclipse with JUnit and mockito to test all the methods including this one. For this specific method, I want to check if the program redirects to one location or another, but I don't know how to do it. Do you have any idea? Thanks.

Upvotes: 2

Views: 3369

Answers (1)

Manuel Leon
Manuel Leon

Reputation: 46

You can actually achieve it easily with Mockito and ArgumentCaptor:

@RunWith(MockitoJUnitRunner.class)
public class MyTest {

   @Mock
   private HttpServletResponse response

   ...

   @Test
   public void testCreatePanelRedirection(){
      ArgumentCaptor<String> captor = ArgumentCaptor.forClass(String.class);
      YourClass.createPanel(request, response, session);
      verify(response).sendRedirect(captor.capture());
      assertEquals("ExpectedURL", captor.getValue());
   }
}

Upvotes: 3

Related Questions