<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 - 一種獨(dú)特的設(shè)計(jì)思路

    jMock Argument Interceptor - 一種獨(dú)特的設(shè)計(jì)思路
    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 閱讀(248) 評(píng)論(0)  編輯  收藏 所屬分類: unit testing


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


    網(wǎng)站導(dǎo)航:
     
    <2025年5月>
    27282930123
    45678910
    11121314151617
    18192021222324
    25262728293031
    1234567

    導(dǎo)航

    統(tǒng)計(jì)

    常用鏈接

    留言簿(2)

    隨筆分類

    隨筆檔案

    文章分類

    文章檔案

    收藏夾

    db2

    dos

    Groovy

    Hibernate

    java

    WAS

    web application

    搜索

    最新評(píng)論

    閱讀排行榜

    評(píng)論排行榜

    主站蜘蛛池模板: 四虎影视在线影院在线观看免费视频 | 亚洲精品色播一区二区| 免费毛片网站在线观看| 久久久久久久久久免免费精品 | 又粗又大又硬又爽的免费视频| 曰批免费视频播放在线看片二| 国产亚洲精品精华液| 我们的2018在线观看免费高清 | 欧洲人免费视频网站在线| 亚洲国产日韩在线| 亚洲?V无码成人精品区日韩 | 精品久久久久久久免费加勒比| 免费中文字幕视频| 激情内射亚洲一区二区三区| 国产美女无遮挡免费视频网站 | 久久精品国产亚洲AV香蕉| 国内外成人免费视频| 国产一级婬片A视频免费观看| 亚洲第一二三四区| 亚洲狠狠爱综合影院婷婷| 免费看h片的网站| 国产特黄特色的大片观看免费视频| 亚洲人成网站18禁止久久影院| 国产精品亚洲高清一区二区| 猫咪社区免费资源在线观看| 国产午夜无码精品免费看动漫| 校园亚洲春色另类小说合集| 亚洲欧洲日产专区| 亚洲成av人在线视| 亚洲第一网站男人都懂| 午夜宅男在线永久免费观看网| 你是我的城池营垒免费观看完整版| 亚洲AV无码成人精品区日韩| 亚洲人成片在线观看| 久久精品国产精品亚洲艾| 亚洲国产精品无码久久九九| 日本人护士免费xxxx视频| 无遮免费网站在线入口| 成人无码a级毛片免费| 一区二区免费电影| 豆国产96在线|亚洲|