少年阿宾

那些青春的岁月

  BlogJava :: 首页 :: 联系 :: 聚合  :: 管理
  500 Posts :: 0 Stories :: 135 Comments :: 0 Trackbacks

package com.abin.lee.servlet.process;

import java.io.IOException;

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;

public class ProcessServlet extends HttpServlet{
 public void init() throws ServletException {
  super.init();
 }
 @SuppressWarnings("rawtypes")
 protected void doPost(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {
  String username=request.getParameter("username");
  String password=request.getParameter("password");
  System.out.println("username="+username);
  System.out.println("password="+password);
  
  ServletContext context = getServletContext();
  RequestDispatcher dispatcher = context.getNamedDispatcher("dispatcher");
  dispatcher.forward(request, response);


//  ServletOutputStream out=response.getOutputStream();
//  BufferedWriter writer=new BufferedWriter(new OutputStreamWriter(out));
//  writer.write("success");
//  writer.flush();
//  writer.close();
  
 }
 public void destroy() {
  super.destroy();
 }
}









package com.abin.lee.servlet.process;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;

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

import junit.framework.TestCase;

import org.easymock.EasyMock;
import org.junit.Before;
import org.junit.Test;
public class ServletMock extends TestCase{
 private static HttpServletRequest request =null;
 private static HttpServletResponse response=null;
 private static ServletContext context=null;
 private static RequestDispatcher dispatcher=null;
 private static ProcessServlet servlet=null;
 @Before
 public  void setUp(){
  request =EasyMock.createMock(HttpServletRequest.class);
  response=EasyMock.createMock(HttpServletResponse.class);
  context=EasyMock.createMock(ServletContext.class);
  dispatcher=EasyMock.createMock(RequestDispatcher.class);
  servlet=new ProcessServlet(){
   private static final long serialVersionUID = 7534303474286669635L;

   public ServletContext getServletContext(){
    return context;
   }
  };
 }
 @Test
 public void test() throws ServletException, IOException{
  EasyMock.expect(request.getParameter("username")).andReturn("abin").times(20000);
  EasyMock.expect(request.getParameter("password")).andReturn("varyall").times(20000);
  EasyMock.expectLastCall();
  EasyMock.replay(request);
  EasyMock.replay(response);
  servlet.doPost(request, response);
  EasyMock.verify(response);
  dispatcher.forward(request, response);
  StringWriter sw=new StringWriter();
  PrintWriter writer=new PrintWriter(sw, true);
  response.getOutputStream();
//  response.set
//  String line="";
//  String result="";
//  while((line=writer.)){
//   
//  }
//  BufferedReader reader=new BufferedReader(new InputStreamReader());
  
  
  
  
 }
}

posted on 2012-11-04 22:29 abin 阅读(734) 评论(0)  编辑  收藏 所属分类: easyMock

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


网站导航:
博客园   IT新闻   Chat2DB   C++博客   博问