lundi 29 juin 2015

JUnit using Mockito for Servlet Test Case

I am try to do a basic mock of my servlet for a JUnit test case. However, I am receiving 0 code coverage from this particular test case. Here is my 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 am testing my doGet method in the servlet in which I am pretty sure the only code that is testable is the response.setContentType("text/html"). However, my test case isn't doing anything. Here is my 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 the folder is success or fail
    for (String emailName : log.getEmails()) {
        if (log.canReadLog(emailName)) {
            System.out.println("Log Successful: " + emailName);
            // Create HTML button to download a successful file
            out.println("<div class='viewButton'>");
            out.println("<form method='get' action=" + emailName +">"); // emailName is the file to 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 to download a failed file
            out.println("<div class='viewButton'>");
            out.println("<form method='get' action=" + emailName +">"); // emailName is the file to 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());
}

}

Aucun commentaire:

Enregistrer un commentaire