java - JUnit using Mockito for Servlet Test Case -


i try basic mock of servlet junit test case. however, receiving 0 code coverage particular test case. here test case:

    @test     public void testservlet() throws servletexception, ioexception {         httpservletrequest request = mock(httpservletrequest.class);                httpservletresponse response = mock(httpservletresponse.class);           downloadservlet servlet = new downloadservlet();         servlet.doget(request, response);         assertequals("text/html", response.getcontenttype());     } 

i testing doget method in servlet in pretty sure code testable response.setcontenttype("text/html"). however, test case isn't doing anything. here downloadservlet class:

package downloadsupport;  import java.io.file; import java.io.ioexception; import java.io.inputstream; import java.io.outputstream; import java.io.printwriter;  import javax.naming.initialcontext; import javax.naming.namingexception; import javax.servlet.servletcontext; import javax.servlet.servletexception; import javax.servlet.annotation.webservlet; import javax.servlet.http.httpservlet; import javax.servlet.http.httpservletrequest; import javax.servlet.http.httpservletresponse;  import com.ibm.ws.security.spnego.context;  import downloadsupport.logread;  /**  * servlet implementation class downloadservlet  * @author gabe  * @version 1.0  */ @webservlet("/downloadservlet") public class downloadservlet extends httpservlet {     private static final long serialversionuid = 1l;  /**  * @see httpservlet#httpservlet()  */ public downloadservlet() {     super(); }  private logread getinstance(){     return new logread(); }  /**  * @see httpservlet#doget(httpservletrequest request, httpservletresponse response)  */ protected void doget(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception {     system.out.println("download servlet invoked");     response.setcontenttype("text/html");      printwriter out = response.getwriter();     logread log = getinstance();     // test if each log file in folder success or fail     (string emailname : log.getemails()) {         if (log.canreadlog(emailname)) {             system.out.println("log successful: " + emailname);             // create html button download successful file             out.println("<div class='viewbutton'>");             out.println("<form method='get' action=" + emailname +">"); // emailname file download             out.println("<button type='submit' class='successbutton'>"                     + "success: " + emailname + "</button>");             out.println("</form>");             out.println("</div>");         }          else {             system.out.println("log failed: " + emailname);             // create html button download failed file             out.println("<div class='viewbutton'>");             out.println("<form method='get' action=" + emailname +">"); // emailname file download             out.println("<button type='submit' class='failbutton'>"                     + "fail: " + emailname + "</button>");             out.println("</form>");             out.println("</div>");         }     }     out.println("<br>");      system.out.println(log.getemails()); } 

}

i suspect test failing nullpointerexception because

response.getwriter(); 

will return null. mockito mock objects return null accessor unless set test properly:

mockito.when(response.getwriter()).thenreturn(aprintwriteryoupreparedearlier); 

also, depending upon log.getemails() returns, code may not enter loop. if happens execute 5 lines , crash @ last out.println("<br>");


Comments

Popular posts from this blog

android - Gradle sync Error:Configuration with name 'default' not found -

java - Andrioid studio start fail: Fatal error initializing 'null' -

html - jQuery UI Sortable - Remove placeholder after item is dropped -