使用HttpServlet写单元测试用例

时间:2017-10-20 21:21:42

标签: unit-testing testing junit mockito powermock

我有这个单元测试,它编译并运行良好。如何在单元测试的测试用例文档中表示这一点(Id,目的,前置条件,输入和预期输出)。输入和输出是什么,因为我不知道httpservletrequest的实际输入,也不知道响应(httpservletresponse)。我只是嘲笑这些对象,所以我可以进行单元测试。我打算做'AssertEquals'但我不知道预期的产量是多少。

@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);
}

我在Controller类中有这个:

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

这在SearchPanelist类中:

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");
    }
}

0 个答案:

没有答案