vendredi 20 octobre 2017

Write Unit test case using HttpServlet

I have this unit test, it compiles and runs well. How can I represent this in a test case document for my unit test (Id, purpose, preconditions, input and expected output). What would be the input and output since I don't know the actual input of the httpservletrequest and don't know either the response (httpservletresponse). I just mocked these objects so I can perform my unit test. I was planning to do 'AssertEquals' but I don't know what could be the expected output.

@Test
public void testSearchPanelists() throws IOException {

    HttpServletRequest req = Mockito.mock(HttpServletRequest.class);
    HttpServletResponse res = Mockito.mock(HttpServletResponse.class);
    HttpSession hs = Mockito.mock(HttpSession.class);

    PowerMockito.mockStatic(SearchPanelist.class);

    Controller.searchPanelists(req, res, hs);
    PowerMockito.verifyStatic(SearchPanelist.class);
    SearchPanelist.searchPanelists(req,res,hs);
}

I have this in Controller class:

public static void searchPanelists(HttpServletRequest req, HttpServletResponse res, HttpSession hs) throws IOException
{
    SearchPanelist.searchPanelists(req, res, hs);
}

and this in SearchPanelist class:

public static void searchPanelists(HttpServletRequest req, HttpServletResponse res, HttpSession hs) throws IOException
{
    HashMap<String, String> searchCriteria = new HashMap<String, String>();

    //Let's pull the values from the search form.        
    searchCriteria.put("FirstName", req.getParameter("pFName"));
    searchCriteria.put("LastName", req.getParameter("pLName"));
    searchCriteria.put("Institution", req.getParameter("pInstitution"));
    searchCriteria.put("Address", req.getParameter("pAddress"));
    searchCriteria.put("City", req.getParameter("pCity"));
    searchCriteria.put("State", req.getParameter("pState"));
    searchCriteria.put("ZipCode", req.getParameter("pZip"));
    searchCriteria.put("Telephone", req.getParameter("pTelephone"));
    searchCriteria.put("Email", req.getParameter("pEmail"));
    searchCriteria.put("Gender", req.getParameter("pGender"));
    searchCriteria.put("Ethnicity", req.getParameter("pEthnicity"));
    searchCriteria.put("Expertise", req.getParameter("pExpertise"));
    searchCriteria.put("ISCID", req.getParameter("pISCID"));

    ArrayList<PanelistProfile> userProfile = DBManager.getPanelists(searchCriteria);

    if(userProfile == null)
        res.sendRedirect("messagePage?messageCode=No Panelists Found.");
    else
    {
        hs.setAttribute("Panelists", userProfile);
        res.sendRedirect("displayPanelists.jsp");
    }
}

Aucun commentaire:

Enregistrer un commentaire