Я пытаюсь проверить свой класс сервлетов на «счастливый поток» с помощью Junit 4. Даже несмотря на то, что тест проходит успешно, охват кода составляет всего 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);
, чтобы после этого поток продолжится.