<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 閱讀(1449) 評論(0)  編輯  收藏 所屬分類: Java

    主站蜘蛛池模板: 久久久久久久国产免费看| ssswww日本免费网站片| 亚洲毛片免费观看| 好看的电影网站亚洲一区| av电影在线免费看| 中文亚洲AV片不卡在线观看| 国产A∨免费精品视频| 亚洲午夜AV无码专区在线播放 | 中文字幕不卡亚洲| 美女无遮挡拍拍拍免费视频| 亚洲综合伊人久久大杳蕉| 热久久这里是精品6免费观看| 亚洲精品无码午夜福利中文字幕| 你懂得的在线观看免费视频| 水蜜桃亚洲一二三四在线| 免费无码一区二区三区| 亚洲a视频在线观看| 免费看美女让人桶尿口| 国产区图片区小说区亚洲区| 国产亚洲色视频在线| 在线看片免费人成视频播| 亚洲资源在线视频| 精品国产免费观看一区| 污网站在线免费观看| 国产成人亚洲综合色影视| 6080午夜一级毛片免费看| 亚洲综合色婷婷在线观看| 亚洲国产专区一区| 日韩在线永久免费播放| 亚洲男人天堂2018av| 亚洲精品美女久久久久99小说| a在线视频免费观看| 亚洲一区二区三区在线网站| 大胆亚洲人体视频| 久久aa毛片免费播放嗯啊| 67194在线午夜亚洲| 亚洲毛片不卡av在线播放一区| 久久免费视频精品| 国产成人精品久久亚洲高清不卡 | 亚洲国产高清人在线| 四虎免费在线观看|