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

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

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

    【轉(zhuǎn)帖】jMock Argument Interceptor - 一種獨特的設(shè)計思路

    jMock Argument Interceptor - 一種獨特的設(shè)計思路
    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


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


    網(wǎng)站導(dǎo)航:
     
    <2025年7月>
    293012345
    6789101112
    13141516171819
    20212223242526
    272829303112
    3456789

    導(dǎo)航

    統(tǒng)計

    常用鏈接

    留言簿(2)

    隨筆分類

    隨筆檔案

    文章分類

    文章檔案

    收藏夾

    db2

    dos

    Groovy

    Hibernate

    java

    WAS

    web application

    搜索

    最新評論

    閱讀排行榜

    評論排行榜

    主站蜘蛛池模板: 学生妹亚洲一区二区| 亚洲午夜无码久久久久| 国产精品成人免费一区二区| 免费观看无遮挡www的视频| 四虎成人免费观看在线网址| 国产免费小视频在线观看| 亚洲人成色77777| 美女被免费网站91色| 永久黄色免费网站| 亚洲国产综合精品中文字幕| 亚洲AV无码一区东京热| 中文字幕亚洲码在线| 黄瓜视频高清在线看免费下载| 亚洲va精品中文字幕| 久久er国产精品免费观看8| 在线观看亚洲精品福利片| 亚洲综合成人婷婷五月网址| 在线观看人成网站深夜免费| 欧美日韩亚洲精品| 97久久免费视频| 久久精品夜色噜噜亚洲A∨| 亚洲大码熟女在线观看| 91免费在线播放| 亚洲乱理伦片在线观看中字| 99久久免费精品高清特色大片| 亚洲国产成人久久综合一区77 | 国产又黄又爽又猛免费app| 亚洲无码视频在线| 国产尤物在线视精品在亚洲| 国产成人福利免费视频| 亚洲日本在线观看| 成人免费一区二区三区| 免费在线不卡视频| 亚洲爆乳成av人在线视菜奈实| 免费A级毛片在线播放不收费| 国产真人无码作爱免费视频| 亚洲精品高清一二区久久| 国产精品99久久免费观看| 亚洲一区日韩高清中文字幕亚洲| 日韩免费在线观看视频| 亚洲经典千人经典日产|