少年阿宾

那些青春的岁月

  BlogJava :: 首页 :: 联系 :: 聚合  :: 管理
  500 Posts :: 0 Stories :: 135 Comments :: 0 Trackbacks
//EasyMockServlet.java
package com.abin.lee.easymock.servlets;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
* Created with IntelliJ IDEA.
* User: abin
* Date: 13-4-22
* Time: 下午3:12
* To change this template use File | Settings | File Templates.
*/
public class EasyMockServlet extends HttpServlet {
    public void doPost(HttpServletRequest request,HttpServletResponse response) throws IOException, ServletException {
        System.out.println("doPost come in");
        String userName=request.getParameter("userName");
        String passWord=request.getParameter("passWord");
        System.out.println("userName="+userName+",passWord="+passWord);
        if("abin".equals(userName)&&"varyall".equals(passWord)){
            System.out.println("come in");
            ServletContext context=this.getServletContext();
            RequestDispatcher dispatcher=context.getNamedDispatcher("dispatcher");
            dispatcher.forward(request,response);
        }else{
            throw new RuntimeException("Login failed.");
        }
    }

}





//EasyMockServletFailedTest.java
package com.abin.lee.easymock.servlets;

import org.easymock.EasyMock;
import org.easymock.IMocksControl;
import org.junit.Test;

import javax.servlet.http.HttpServletRequest;

import static junit.framework.TestCase.assertEquals;
import static org.junit.Assert.fail;

/**
* Created with IntelliJ IDEA.
* User: abin
* Date: 13-4-22
* Time: 下午5:08
* To change this template use File | Settings | File Templates.
*/
public class EasyMockServletFailedTest {
    @Test
    public void testEasyMockServletFailed(){
        HttpServletRequest request=EasyMock.createMock(HttpServletRequest.class);
        EasyMock.expect(request.getParameter("userName")).andReturn("abin");
        EasyMock.expect(request.getParameter("passWord")).andReturn("varyall").times(1);
        EasyMock.replay(request);
        EasyMockServlet easyMockServlet=new EasyMockServlet();
        try {
              easyMockServlet.doPost(request,null);
              fail("Not caught exception!");
        }catch(Exception e){
            assertEquals("Login failed.", e.getMessage());
            e.printStackTrace();
        }
        EasyMock.verify(request);
    }
}






//EasyMockServletSuccessTest
package com.abin.lee.easymock.servlets;

import org.easymock.EasyMock;
import org.easymock.IMocksControl;
import org.junit.Test;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
* Created with IntelliJ IDEA.
* User: abin
* Date: 13-4-22
* Time: 下午7:49
* To change this template use File | Settings | File Templates.
*/
public class EasyMockServletSuccessTest {
    @Test
    public void testEasyMockServletSuccess() throws ServletException, IOException {
        IMocksControl control= EasyMock.createControl();
        HttpServletRequest request=control.createMock(HttpServletRequest.class);
//        HttpServletResponse response=control.createMock(HttpServletResponse.class);
        final ServletContext servletContext=control.createMock(ServletContext.class);
        RequestDispatcher requestDispatcher=control.createMock(RequestDispatcher.class);

        EasyMock.expect(request.getParameter("userName")).andReturn("abin").once();
        EasyMock.expect(request.getParameter("passWord")).andReturn("varyall").once();
        EasyMock.expect(servletContext.getNamedDispatcher("dispatcher")).andReturn(requestDispatcher).times(1);

        requestDispatcher.forward(request,null);
        EasyMock.expectLastCall();

        control.replay();

        EasyMockServlet easyMockServlet=new EasyMockServlet(){
           public ServletContext getServletContext(){
               return servletContext;
           }
        } ;

        easyMockServlet.doPost(request,null);
        control.verify();


    }
}
posted on 2013-04-23 13:27 abin 阅读(748) 评论(0)  编辑  收藏 所属分类: easyMock

只有注册用户登录后才能发表评论。


网站导航: