51Testing软件测试论坛

标题: HttpUnit测试Servlet [打印本页]

作者: javazwj    时间: 2011-9-16 15:12
标题: HttpUnit测试Servlet
待测类:
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);
        }
}

源码见附件




欢迎光临 51Testing软件测试论坛 (http://bbs.51testing.com/) Powered by Discuz! X3.2