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

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

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

    【轉帖】jMock Argument Interceptor - 一種獨特的設計思路

    jMock Argument Interceptor - 一種獨特的設計思路
    Motivation
    An Object you need to test is constructing another complex object internally which you cannot access and this object is passed to a collaborator you can replace with a mock.
    Solution
    Write an Interceptor (a custom jMock Stub ) to intercept the argument passed to the mocked method. The Interceptor exposes the argument, allowing for standard jUnit assertions.
    Alternatives

        * Use a custom jMock Constraint when the argument's asserted state is simple.
        * Use a Fake Object when a class cannot be dynamically mocked.

    Example

    public void testInterceptArgument() {

        List arguments = new ArrayList();

        Mock mockSubmissionTracker = mock(SubmissionTracker.class);
        mockSubmissionTracker.expects(once()).method("record").will(captureArgumentsIn(arguments));

        SecretLottery lotto = new SecretLottery( (SubmissionTracker)mockSubmissionTracker.proxy());

        lotto.createTicket();

        LotteryTicket ticket = (LotteryTicket) arguments.get(0);
        assertEquals("Secret Number", 12345, ticket.number);
    }

    private Stub captureArgumentsIn(List argumentList) {

        return new ArgumentInterceptor(argumentList);

    }

    class ArgumentInterceptor implements Stub {

        List arguments;

        public ArgumentInterceptor(List argumentList) {
            arguments = argumentList;
        }

        public Object invoke(Invocation invocation) throws Throwable {
            arguments.addAll(invocation.parameterValues);
            return null;
        }

        public StringBuffer describeTo(StringBuffer buffer) {
            return buffer;
        }
    }

    The Traditional jMock approach
    The traditional way to handle this with jMock is to create a custom Constraint to verify the argument passed into the mocked method. Unfortunatly, this technique can produce a less explict test and a lot of supporting code. The above example handled with a traditional jMock Constraint would look like the following...

    public void testInterceptArgument() {

        Mock mockSubmissionTracker = mock(SubmissionTracker.class);
        mockSubmissionTracker.expects(once()).method("record").with(ticketNumber(12345));

        SecretLottery lotto = new SecretLottery( (SubmissionTracker) mockSubmissionTracker.proxy());

        lotto.createTicket();
    }

    private Constraint ticketNumber(final int ticketNumber) {

        return new Constraint() {

            public boolean eval(Object arg) {

                LotteryTicket ticket = (LotteryTicket) arg;  
                return ticketNumber == ticket.number;
            }

            public StringBuffer describeTo(StringBuffer buffer) {
                return buffer.append(ticketNumber);
            }
        }
    }

    While this example works fairly well, each new test method would likely require its own Constraint. When the verification in the eval method becomes complex, it can become difficult to determine why a test is failing. An Argument Interceptor allows for traditional jUnit assertions, providing clearer failures for complex verfications.

    posted on 2011-01-31 22:24 koradji 閱讀(258) 評論(0)  編輯  收藏 所屬分類: unit testing


    只有注冊用戶登錄后才能發表評論。


    網站導航:
     
    <2025年7月>
    293012345
    6789101112
    13141516171819
    20212223242526
    272829303112
    3456789

    導航

    統計

    常用鏈接

    留言簿(2)

    隨筆分類

    隨筆檔案

    文章分類

    文章檔案

    收藏夾

    db2

    dos

    Groovy

    Hibernate

    java

    WAS

    web application

    搜索

    最新評論

    閱讀排行榜

    評論排行榜

    主站蜘蛛池模板: 免费的黄网站男人的天堂| 亚洲综合一区二区精品导航| 亚洲一区二区三区在线观看蜜桃 | 国产精品亚洲а∨无码播放麻豆| 两性刺激生活片免费视频| 亚洲理论在线观看| 99久久99这里只有免费费精品| 亚洲美女中文字幕| 久久WWW色情成人免费观看| 伊人久久五月丁香综合中文亚洲 | 午夜a级成人免费毛片| 亚洲偷自拍另类图片二区| 大学生高清一级毛片免费| 午夜亚洲WWW湿好爽 | 亚洲天天在线日亚洲洲精| 最近中文字幕无免费| 亚洲妓女综合网99| 成人午夜视频免费| 国产精品亚洲专区在线播放| 久久99亚洲综合精品首页| 青青操视频在线免费观看| 久久99亚洲网美利坚合众国| 亚洲免费网站观看视频| 久久精品国产亚洲AV未满十八| 亚洲av日韩片在线观看| 日韩成人免费视频| xxx毛茸茸的亚洲| 亚洲国产av无码精品| 免费人成在线观看网站| 亚洲同性男gay网站在线观看| 免费看大美女大黄大色| 三上悠亚电影全集免费 | 免费人成视频x8x8入口| a级成人毛片免费视频高清| 亚洲第一网站免费视频| 国产精品二区三区免费播放心| 中文字幕一区二区三区免费视频| 亚洲视频在线观看视频| 免费吃奶摸下激烈视频| 57pao国产成视频免费播放| 精品韩国亚洲av无码不卡区 |