【问题标题】:Happy Flow Test case using Junit 4 for java servlet使用 Junit 4 for java servlet 的快乐流测试用例
【发布时间】:2020-02-21 10:54:29
【问题描述】:

我正在尝试使用 Junit 4 测试我的 servlet 类的快乐流程。即使测试通过了代码覆盖率也只有 37%。需要帮助以了解问题所在以及如何纠正它。

测试用例

public class SearchOrganizationTest extends Mockito{

    private final ByteArrayOutputStream outContent = new ByteArrayOutputStream();
    private final ByteArrayOutputStream errContent = new ByteArrayOutputStream();
    private final PrintStream originalOut = System.out;
    private final PrintStream originalErr = System.err;

    @Before
    public void setUpStreams() {
        System.setOut(new PrintStream(outContent));
        System.setErr(new PrintStream(errContent));
    }
    @After 
    public void restoreStreams() {
        System.setOut(originalOut);
        System.setErr(originalErr);
    }
    @Mock
    private Organization org;
    @InjectMocks
    SearchOrganization mockSearchOrganization;
    @Test //Test code for checking whether the Search Organization outer (invoking) function is working fine
    public void testServlet() throws Exception {
        HttpServletRequest request = mock(HttpServletRequest.class);       
        HttpServletResponse response = mock(HttpServletResponse.class);

        try {
            StringWriter stringWriter = new StringWriter();
            PrintWriter writer = new PrintWriter(stringWriter);
            when(response.getWriter()).thenReturn(writer);

            new SearchOrganization().doPost(request, response); //calling the function with the dummy parameters
            System.out.print("hello");
            String res = outContent.toString();
            assertEquals(res, outContent.toString());
            System.err.print("hello again");
            assertEquals("hello again", errContent.toString());

            writer.flush(); // it may not have been flushed yet...
        }catch(Exception e){}
    }
}

SearchOrganization.class

//Invoking the method on the submission of the form
@WebServlet("/viewOrganization")
public class SearchOrganization extends HttpServlet {
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html");
        RequestDispatcher dispatcher=request.getRequestDispatcher("SearchOrg.html");
        PrintWriter out=response.getWriter();
        //Getting the attributes from the UI

        try {
            DataConnection ds =new DataConnection();
            String org_name = request.getParameter("searchOrg");

            //Setting the objects to insert the achieved attributes to corresponding the columns of the table

            Organization searchOrg = new Organization();

            searchOrg.setOrgName(org_name);
            dispatcher.include(request, response);
            //calling and fetching the details recieved by the getOrganizations function from OrganizationDao class
            List<Organization> list = new OrganizationDao(ds).getOrganization(searchOrg);
            out.print("<h3 align = 'center'>Orgnanization Name</h3>");
            out.print("<table class= 'fixed_header' align= 'center' ");
            out.print("<thead>");
            out.print("</thead>");
            out.print("<tbody");  
            //listing the values fetched by the function to appropriate columns of a table 
            for(Organization e:list){  
                out.print("<tr><td style='padding:10px'><a href='SearchOrg.html?orgName="+e.getOrgName()+"'>"+e.getOrgName()+"</a></td></tr>");
            }  

            out.print("</tbody>");  
            out.print("</table>");

            out.close();
        }catch(Exception e) {System.out.println("SearchOrganizatin: doPost() - "+e);}
    }
}

*这里dispatcher.include(request, response);的测试用例打破了快乐的流程,直接进入catch(Exception e) {System.out.println("SearchOrganizatin: doPost() - "+e);}部分。

我需要知道如何忽略dispatcher.include(request, response);,以便之后继续流程。

【问题讨论】:

    标签: java unit-testing servlets code-coverage junit4


    【解决方案1】:

    您需要处理模拟的request 对象并在调用getParameter 时返回一个模拟的调度程序。

    类似

    when(request.getParameter(Mockito.anyString())).thenReturn(mocked-dispatcher-object);
    

    【讨论】:

    • 你能否详细说明一下,以便我可以通过这段代码理解发生了什么。我是这个@Smile的新手
    • 我尝试创建一个模拟调度程序对象,但是当我尝试when(request.getParameter(Mockito.anyString())).thenReturn(mocked-dispatcher-object); 时,它说The method thenReturn(String) in the type OngoingStubbing&lt;String&gt; is not applicable for the arguments (RequestDispatcher),如果我将其更改为字符串,则测试用例通过但代码覆盖率没有得到改善@微笑
    • 您可能想在stackoverflow.com/questions/22357046/…阅读问题/答案
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-07-12
    • 2015-10-19
    • 2011-07-23
    • 2019-04-18
    • 1970-01-01
    • 2013-04-08
    相关资源
    最近更新 更多