|
待测类:
package testEasyMock;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class MyEasymock {
public void execute(HttpServletRequest request,
HttpServletResponse response) throws Exception {
String tablename = request.getParameter("tablename");
System.out.println("talename is: "+tablename);
//这里就报错了
request.setAttribute("name", "lucy");
}
}
测试类:
package testEasyMock;
import static org.easymock.EasyMock.createMock;
import static org.easymock.EasyMock.expectLastCall;
import static org.easymock.EasyMock.replay;
import static org.easymock.EasyMock.verify;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
public class TestMyEasymock {
private MyEasymock mock;
private HttpServletRequest mockRequest;
private HttpServletResponse mockResponse;
@Before
public void setUp() {
mockRequest = createMock(HttpServletRequest.class);
mockResponse = createMock(HttpServletResponse.class);
}
@Test
public void testExecute() throws IOException, ServletException {
mock = new MyEasymock();
mockRequest.getParameter("tablename");
expectLastCall().andReturn("DEPARTMENT");
replay(mockRequest);
replay(mockResponse);
try {
mock.execute(mockRequest, mockResponse);
} catch (Exception e) {
e.printStackTrace();
}
}
@After
public void tearDown() {
verify(mockRequest);
verify(mockResponse);
}
}
源码见附件 |
|