半步多 玄玉V笔记

EasyMock测试Servlet

2013-07-09
玄玉

实际应该用Cactus框架来测试Servlet,详见我的另一篇博文:https://www.xuanyuv.com/blog/20130711/cactus-servlet.html

这里只是用EasyMock模拟HttpServletRequest等对象

下面是演示代码

首先是用到的实体类User.Java

package com.xuanyuv.model;
public class User {
    private int id;
    private String username;
    private String password;
    /*-- 三个属性的setter和getter略 --*/
    public User(){}
    public User(int id, String username, String password) {
        this.id = id;
        this.username = username;
        this.password = password;
    }
}

接下来是待测试的LoginServlet.java

package com.xuanyuv.servlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import com.xuanyuv.model.User;

public class LoginServlet extends HttpServlet {
    private static final long serialVersionUID = 6655227641354075528L;
    public boolean isLogin(HttpServletRequest request){
        HttpSession session = request.getSession();
        if(null == session){
            return false;
        }
        User user = (User)session.getAttribute("loginUser");
        if(null == user){
            return false;
        }
        return true;
    }
}

最后是使用EasyMock编写的测试用例LoginServletTest.java

package com.xuanyuv.servlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.easymock.EasyMock;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import com.xuanyuv.model.User;

/**
 * EasyMock测试Servlet
 * 虽然示例代码是一个WebProject,但不需配置web.xml,因为EasyMock用不上它
 * Created by 玄玉<https://www.xuanyuv.com/> on 2013/07/09 14:30.
 */
public class LoginServletTest {
    private LoginServlet servlet;
    private HttpServletRequest request;
    private HttpSession session;

    @Before
    public void init(){
        servlet = new LoginServlet();
        request = EasyMock.createStrictMock(HttpServletRequest.class);
        session = EasyMock.createStrictMock(HttpSession.class);
    }

    /**
     * 测试登录失败的情况(session为空)
     */
    @Test
    public void testIsLoginFailSessionIsNull(){
        //声明HttpServletRequest获取到的HttpSession是空的
        EasyMock.expect(request.getSession()).andReturn(null);
        EasyMock.replay(request, session);
        //验证HttpSession为空时登录应该是失败的
        Assert.assertFalse(servlet.isLogin(request));
        EasyMock.verify(request, session);
    }

    /**
     * 测试登录失败的情况(session中无用户)
     */
    @Test
    public void testIsLoginFailSessionNoUser(){
        //声明HttpServletRequest获取到的是正常的HttpSession
        EasyMock.expect(request.getSession()).andReturn(session);
        //声明HttpSession中无用户
        EasyMock.expect(session.getAttribute("loginUser")).andReturn(null);
        EasyMock.replay(request, session);
        //验证HttpSession中无用户时登录应该是失败的
        Assert.assertFalse(servlet.isLogin(request));
        EasyMock.verify(request, session);
    }

    /**
     * 测试登录成功的情况
     */
    @Test
    public void testIsLoginSuccess(){
        //声明HttpServletRequest获取到的是正常的HttpSession
        EasyMock.expect(request.getSession()).andReturn(session);
        //声明HttpSession中存在用户
        EasyMock.expect(session.getAttribute("loginUser")).andReturn(new User());
        EasyMock.replay(request, session);
        //验证HttpSession有效且存在用户时登录应该是成功的
        Assert.assertTrue(servlet.isLogin(request));
        EasyMock.verify(request, session);
    }
}

Content