關於easymock的模擬servlet的doPost的方法的淺例

藍色王者發表於2013-04-08

import java.io.IOException;

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

import junit.framework.TestCase;

import org.easymock.EasyMock;

import com.huawei.servlet.getData;

public class TestGetDataServlet extends TestCase {

 public void testDoPost()
 {
  HttpServletRequest request = EasyMock
    .createMock(HttpServletRequest.class);
  EasyMock.expect(request.getParameter("username")).andReturn("aaaa")
    .anyTimes();
  EasyMock.replay(request);
  getData servlet = new getData();

  try {
   servlet.doPost(request, null);
  } catch (ServletException e) {
   e.printStackTrace();
  } catch (IOException e) {
   e.printStackTrace();
  }

  EasyMock.verify(request);

 }
}

相關文章