목차

Servlet API Mock Sample Source Code

Test Class

 
package egovframework.guideprogram.test.testcase.mock;
 
import junit.framework.Assert;
 
import org.junit.Before;
import org.junit.Test;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpServletResponse;
 
import egovframework.guideprogram.test.target.mock.Servlet;
 
public class HttpRequestMockTest {
 
	/**
	 * 서블릿 호출에 필요한 Request 객체에 대한 Mocking 클래스
	 */
	private MockHttpServletRequest request;
 
	/**
	 * 서블릿 호출에 필요한 Response 객체에 대한 Mocking 클래스
	 */
	private MockHttpServletResponse response;
 
	/**
	 * 서블릿 호출 테스트를 위한 대상 테스트 클래스
	 */
	private Servlet servlet;
 
	/**
	 * 서블릿 호출을 위한 Mocking 클래스를 생성하는 초기 셋업 메소드
	 */
	@Before
	public void setUp() {
		servlet  = new Servlet();
 
		request  = new MockHttpServletRequest();
		response = new MockHttpServletResponse();
	}
 
 
    /**
     * 서블릿 호출을 수행해보는 테스트 메소드
     */
    @Test
    public void testCallMyServlet() throws Exception
    {
        servlet.service(request, response);
        Assert.assertEquals(servlet.getClass().getSimpleName(), (String) request.getAttribute("res"));
    }
 
}
 
package egovframework.guideprogram.test.testcase.mock;
 
 
import static org.junit.Assert.assertTrue;
 
import org.junit.Before;
import org.junit.Test;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpServletResponse;
import org.springframework.mock.web.MockHttpSession;
 
import egovframework.guideprogram.test.target.mock.Servlet;
 
 
public class SessionMockTest {
    /** mock object of the HttpServletRequest */
    private MockHttpServletRequest request;
 
    /** mock object of the HttpServletResponse */
    private MockHttpServletResponse response;
 
    /** mock object of the HttpSession */
    private MockHttpSession session;
 
    /** Target Class */
    private Servlet servlet;
 
    /**
     * 각 Test 메소드 시작 전마다 수행되는 메소드<br>
     * 테스트에 사용될 test fixture를 여기에 준비한다.
     * 
     * @throws java.lang.Exception
     */
    @Before
    public void setUp() throws Exception
    {
        servlet  = new Servlet();
 
        request  = new MockHttpServletRequest();
        session = new MockHttpSession();
    }
 
    /**
     * test if the given information is a administrator
     *
     * @throws Exception
    */
    @Test
    public void testConfirmAdmin() throws Exception {
        session.setAttribute("userid", "administrator");
        session.setAttribute("password", "1234");
 
        request.setSession(session);
        servlet.confirmAdmin(request, response);
 
        assertTrue(servlet.isAdmin());
    }
 
    /**
     * test if the given information is not a administrator
     *
     * @throws Exception
    */
    @Test
    public void testConfirmNotAdmin() throws Exception {
        session.setAttribute("userid", "admin");
        session.setAttribute("password", "0000");
 
        request.setSession(session);
        servlet.confirmAdmin(request, response);
 
        assertTrue(!servlet.isAdmin());
    }
}

Target Class

 
package egovframework.guideprogram.test.target.mock;
 
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
 
/*
 * Test Case 를 위한 대상 Test Object 로서 서블릿 요청을 처리하는 클래스
 */
public class Servlet extends HttpServlet {
 
    /** The logger instance for this class */
    private final Log logger = LogFactory.getLog(getClass());
 
    /** isAdmin */
    private boolean isAdmin = false;
 
    /**
     * 디폴트 메소드
     *
     * @param request
     * @param response
     * @throws ServletException
     * @see javax.servlet.http.HttpServlet#service(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
    */
    public void service(HttpServletRequest request, HttpServletResponse response)
            throws ServletException {
        request.setAttribute("res", this.getClass().getSimpleName());
    }
 
    /**
     * Admin 확인
     *
     * @param request
     * @param response
     * @throws ServletException
    */
    public void confirmAdmin(HttpServletRequest request, HttpServletResponse response)
            throws ServletException {
        HttpSession session = request.getSession();
        String userid = (String) session.getAttribute("userid");
        String password = (String) session.getAttribute("password");
 
        if ("administrator".equals(userid) && "1234".equals(password)) 
        {
           logger.debug("You're administrator");
           this.isAdmin = true;
        } else 
        {
            this.isAdmin = false;
        }
    }
 
 
    /**
     * isAdmin
     * 
     * @return the isAdmin
     */
    public boolean isAdmin()
    {
        return isAdmin;
    }
 
 
    /**
     * @param isAdmin the isAdmin to set
     */
    public void setAdmin(boolean isAdmin)
    {
        this.isAdmin = isAdmin;
    }
}