zoukankan      html  css  js  c++  java
  • 使用EasyMock对Servlet进行简单的测试

    这是一个WebProject,但不需配置web.xml,因为EasyMock用不上它


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

    package com.jadyer.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.jadyer.servlet;
    
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpSession;
    
    import com.jadyer.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.jadyer.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.jadyer.model.User;
    
    /**
     * EasyMock框架在使用时只需要一个easymock-3.1.jar 
     * 下载地址为http://sourceforge.net/projects/easymock/files/
     * @create Jul 9, 2013 2:28:41 PM
     * @author 玄玉
      
       
     */
    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);
    	}
    }
      
  • 相关阅读:
    使用Python创建TCP代理之工业时代造轮子
    CVE-2020-0796 SMB远程代码执行漏洞(分析、验证及加固)
    Oracle 找到引起账户锁定的IP
    【OGG 故障处理】OGG-01031
    【OGG 故障处理】OGG-01028
    【OGG 故障处理】 丢失归档恢复
    19C imp 导入合并表空间
    CentOS 7 配置VNCServer
    ORA-3136 问题处理
    HugePages概述--翻译自19C文档
  • 原文地址:https://www.cnblogs.com/jiangu66/p/3181494.html
Copyright © 2011-2022 走看看