[英]Happy Flow Test case using Junit 4 for java servlet
我正在嘗試使用 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);
以便在此之后繼續流程。
您需要處理模擬的request
對象並在調用getParameter
時返回模擬的調度程序。
就像是
when(request.getParameter(Mockito.anyString())).thenReturn(mocked-dispatcher-object);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.