<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    honzeland

    記錄點滴。。。

    常用鏈接

    統計

    Famous Websites

    Java

    Linux

    P2P

    最新評論

    JMock Examples

    The following is a simple example of a servlet we will write unit tests for:

    The servlet:
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import javax.servlet.ServletException;
    import java.io.IOException;
    import java.util.Map;

    public class MyServlet extends HttpServlet {
        private MyService myService;
        public void setMyService(MyService myService) {
            this.myService = myService;
        }

        public void process(HttpServletRequest request, HttpServletResponse response) {
            String action = request.getParameter("action");
            if ("update".equals(action)) {
                Map parameters = request.getParameterMap();
                try {
                    myService.updateModel(parameters);
                } catch (MyException e) {
                    request.setAttribute("error", e.getMessage());
                }
            }
            request.setAttribute("model", myService.getModel());
            getServletContext().getRequestDispatcher("myView.jsp").forward(request, response);
        }

        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            process(request, response);
        }
    }

    The unit test:
        * if the action is update, does it call myService.updateModel (example is below)
        * if myService.updateModel throws an exception does the response contain the exceptions message (not implemented below)
    import org.jmock.Mock;
    import org.jmock.MockObjectTestCase;
    public class MyServletTest extends MockObjectTestCase {
        private Mock mockMyService;
        private Mock mockRequest;
        private Mock mockResponse;
        private MyService myService;
        private HttpServletRequest request;
        private HttpServletResponse response;
        private MyServlet myServlet;
        
        protected void setUp() throws java.lang.Exception {
            mockMyService = mock(MyService.class);
            mockRequest = mock(HttpServletRequest.class);
            mockResponse = mock(HttpServletResponse.class);
            myService = (MyService) mockMyService.proxy();
            request = (HttpServletRequest) mockRequest.proxy();
            response = (HttpServletResponse) mockResponse.proxy();
            myServlet = new MyServlet();
        }

        public void testUpdate() {
            // SETUP ANY OBJECTS USED ONLY IN THIS TESTCASE
            Map parameters = new Hashtable();
            // SETUP THE EXPECTED CALLS TO THE MOCK OBJECTS
            // setup the mock request object to have the getParameter
            // method called once with argument being equal to the
            // string action and have it return the string update
            mockRequest.expects(once()).method("getParameter").
                with(eq("action")).will(returnValue("update"));
            // setup the mock request object to have the getParameterMap
            // method called once with no arguments and have it
            // return an empty map
            mockRequest.expects(once()).method("getParameterMap")
                .will(returnValue(parameters));
            // setup the mock myService object to have the updateModel
            // method called once with the parameters map as the
            // argument
            mockMyService.expects(once()).method("updateModel").with(same(parameters));
            // setup the mock myService object to have the getModel
            // method called once with no arguments and have it
            // return an empty map
            mockMyService.expects(once()).method("getModel")
                .will(returnValue(parameters));
            // setup the mock myService object to have the updateModel
            // method called once with the parameters map as the
            // argument
            mockRequest.expects(once()).method("setAttribute").with(eq("model"), same(parameters));
            // RUN THE TEST
            // add the myService mock object to the servlet so
            // that it can handle calls later
            myServlet.setMyService(myService);
            myServlet.process(request, response);
        }
    }



    posted on 2007-12-13 12:45 honzeland 閱讀(1441) 評論(0)  編輯  收藏 所屬分類: Java

    主站蜘蛛池模板: 亚洲精品无码午夜福利中文字幕| 亚洲av永久无码精品天堂久久| 久久精品亚洲福利| 亚洲黄色免费在线观看| 亚洲1区2区3区精华液| 久久大香伊焦在人线免费| 无码国模国产在线观看免费| 亚洲国产精品无码中文字| 国产精品观看在线亚洲人成网| 无码av免费网站| 亚洲男人av香蕉爽爽爽爽| 久久久久亚洲国产| 最近中文字幕电影大全免费版 | 中文字幕亚洲第一在线| 3344永久在线观看视频免费首页| 亚洲人成网站18禁止一区| 亚洲免费日韩无码系列| 免费国产怡红院在线观看| 亚洲五月丁香综合视频| 久久国产精品成人片免费| 亚洲黄色中文字幕| 全免费一级午夜毛片| 亚洲冬月枫中文字幕在线看| 全免费a级毛片免费看| 91亚洲一区二区在线观看不卡| 在线天堂免费观看.WWW | 亚洲综合小说久久另类区| 免费在线观看一级片| 中文字幕亚洲不卡在线亚瑟| 国产精品免费大片| jlzzjlzz亚洲jzjzjz| 国产中文字幕免费| 黄色免费在线网址| 四虎AV永久在线精品免费观看| 中国好声音第二季免费播放| 亚洲综合伊人久久综合| 香蕉免费看一区二区三区| 亚洲毛片网址在线观看中文字幕| 好久久免费视频高清| 亚洲日韩国产二区无码| 免费涩涩在线视频网|