import javax.servlet.*;  
import javax.servlet.http.*;  
import org.easymock.*;  
  
public class LoginServletTest {  
  // 测试登陆失败  
  @Test  
   public void testLoginFailed() throws Exception {  
        // 使用 EasyMock 生成 Mock 对象；  
        MockControl mc = MockControl.createControl(HttpServletRequest.class);  
        HttpServletRequest request = (HttpServletRequest)mc.getMock();  
        // 设定 Mock 对象的预期行为和输出；  
        request.getParameter("username");  
        mc.setReturnValue("admin", 1);  
        request.getParameter("password");  
        mc.setReturnValue("1234", 1);  
        // 将 Mock 对象切换到 Replay 状态；  
        mc.replay();  
        // now start test:  
        LoginServlet servlet = new LoginServlet();  
        try {  
              // 里面会调用 Mock 对象方法进行单元测试；  
            servlet.doPost(request, null);  
            fail("Not caught exception!");  
        }  
        catch(RuntimeException re) {  
            assertEquals("Login failed.", re.getMessage());  
        }  
        // 对 Mock 对象的行为进行验证。  
        mc.verify();  
    }  

@@ -1051,16 +1051,1729 @@
 fy();  %0A    %7D  %0A
+  %0A   // %E6%B5%8B%E8%AF%95%E7%99%BB%E9%99%86%E6%88%90%E5%8A%9F  %0A    @Test  %0A    public void testLoginOK() throws Exception %7B  %0A        // %E4%BD%BF%E7%94%A8 EasyMock %E7%94%9F%E6%88%90 Mock %E5%AF%B9%E8%B1%A1%EF%BC%9B  %0A        MockControl requestCtrl = MockControl.createControl(HttpServletRequest.class);  %0A        HttpServletRequest requestObj = (HttpServletRequest)requestCtrl.getMock();  %0A        MockControl contextCtrl = MockControl.createControl(ServletContext.class);  %0A        final ServletContext contextObj = (ServletContext)contextCtrl.getMock();  %0A        MockControl dispatcherCtrl = MockControl.createControl(RequestDispatcher.class);  %0A        RequestDispatcher dispatcherObj = (RequestDispatcher)dispatcherCtrl.getMock();  %0A       // %E8%AE%BE%E5%AE%9A Mock %E5%AF%B9%E8%B1%A1%E7%9A%84%E9%A2%84%E6%9C%9F%E8%A1%8C%E4%B8%BA%E5%92%8C%E8%BE%93%E5%87%BA%EF%BC%9B  %0A        requestObj.getParameter(%22username%22);  %0A        requestCtrl.setReturnValue(%22admin%22, 1);  %0A        requestObj.getParameter(%22password%22);  %0A        requestCtrl.setReturnValue(%22123456%22, 1);  %0A        contextObj.getNamedDispatcher(%22dispatcher%22);  %0A        contextCtrl.setReturnValue(dispatcherObj, 1);  %0A        dispatcherObj.forward(requestObj, null);  %0A        dispatcherCtrl.setVoidCallable(1);  %0A        // %E5%B0%86 Mock %E5%AF%B9%E8%B1%A1%E5%88%87%E6%8D%A2%E5%88%B0 Replay %E7%8A%B6%E6%80%81%EF%BC%9B  %0A        requestCtrl.replay();  %0A        contextCtrl.replay();  %0A        dispatcherCtrl.replay();  %0A          // %E9%87%8C%E9%9D%A2%E4%BC%9A%E8%B0%83%E7%94%A8 Mock %E5%AF%B9%E8%B1%A1%E6%96%B9%E6%B3%95%E8%BF%9B%E8%A1%8C%E5%8D%95%E5%85%83%E6%B5%8B%E8%AF%95%EF%BC%9B  %0A        //%E4%B8%BA%E4%BA%86%E8%AE%A9getServletContext()%E6%96%B9%E6%B3%95%E8%BF%94%E5%9B%9E%E6%88%91%E4%BB%AC%E5%88%9B%E5%BB%BA%E7%9A%84ServletContext Mock%E5%AF%B9%E8%B1%A1%EF%BC%8C%E6%88%91%E4%BB%AC%E5%AE%9A%E4%B9%89%E4%B8%80%E4%B8%AA%E5%8C%BF%E5%90%8D%E7%B1%BB%E5%B9%B6%E8%A6%86%E5%86%99getServletContext()%E6%96%B9%E6%B3%95%EF%BC%9A  %0A        LoginServlet servlet = new LoginServlet() %7B  %0A            public ServletContext getServletContext() %7B  %0A                return contextObj;  %0A            %7D  %0A        %7D;  %0A        servlet.doPost(requestObj, null);  %0A         // %E5%AF%B9 Mock %E5%AF%B9%E8%B1%A1%E7%9A%84%E8%A1%8C%E4%B8%BA%E8%BF%9B%E8%A1%8C%E9%AA%8C%E8%AF%81%E3%80%82  %0A        requestCtrl.verify();  %0A        contextCtrl.verify();  %0A        dispatcherCtrl.verify();  %0A    %7D  %0A%7D  %0A

Update LoginServletTest.java