对于要在项目里对servlet进行单元测试,可以使用junit进行,而用junit测试servlet时有两种方法,一种使用httpunit + servlet测试,另一种是easymock + servlet,在这里重点介绍使用easymock方法进行的junit测试。


参考地址:https://www.ibm.com/developerworks/cn/opensource/os-cn-easymock/ 


 以下是个人使用java工程测试servlet案例

 

引入相关jar

<!-- easymock -->
		<dependency>
		    <groupId>org.easymock</groupId>
		    <artifactId>easymock</artifactId>
		    <version>${easymock.version}</version>
		    <scope>test</scope>
		</dependency>
		<!-- servlet-api -->
		<dependency>
		    <groupId>org.apache.tomcat</groupId>
		    <artifactId>servlet-api</artifactId>
		    <version>${servletapi.version}</version>
		</dependency>

测试类:ResultServletTest.java

package com.noteshare.easymock;

import static org.easymock.EasyMock.createMock;
import static org.easymock.EasyMock.expectLastCall;
import static org.easymock.EasyMock.replay;
import static org.easymock.EasyMock.verify;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

public class ResultServletTest {
    private PostServlet servlet;
    private HttpServletRequest mockRequest;
    private HttpServletResponse mockResponse;

    @Before
    public void setUp() {
        servlet = new PostServlet();
        mockRequest = createMock(HttpServletRequest.class); // 加载
        mockResponse = createMock(HttpServletResponse.class);
    }

    @After
    public void tearDown() {
        verify(mockRequest); // 验证
        verify(mockResponse);
    }

    @Test
    public void testDoPostHttpServletRequestHttpServletResponse() throws ServletException, IOException {
        mockRequest.getParameter("username"); // 传入参数
        expectLastCall().andReturn("1");

        mockRequest.getParameter("password"); // 传入参数
        expectLastCall().andReturn("chevy");
        
        replay(mockRequest); // 回放
        replay(mockResponse);
        servlet.doPost(mockRequest, mockResponse); // 调用
    }

}
以下是要测试的servlet类:PostServlet.java

package com.noteshare.easymock;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class PostServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException {
        System.out.println("调用doPost方法");
        doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException {
        System.out.println("调用doGet方法");
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        System.out.println("username=" + username);
        System.out.println("password=" + password);
        System.out.println("servlet test success....");
    }
}



以下是测试结果:


Junit 测试 servlet--easymock





作者:星辰 时间:2017-07-06 浏览 647评论 0 赞 0砸 0 标签: 测试 Java基础知识
评论
还可以再输入500个字

请您注意

·自觉遵守:爱国、守法、自律、真实、文明的原则
·尊重网上道德,遵守《全国人大常委会关于维护互联网安全的决定》及中华人民共和国其他各项有关法律法规
·严禁发表危害国家安全,破坏民族团结、国家宗教政策和社会稳定,含侮辱、诽谤、教唆、淫秽等内容的作品
·承担一切因您的行为而直接或间接导致的民事或刑事法律责任
·您在NoteShare上发表的作品,NoteShare有权在网站内保留、转载、引用或者删除
·参与本评论即表明您已经阅读并接受上述条款