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

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

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

    thinking

    one platform thousands thinking

    Fun with EasyMock

    Fun with EasyMock

    I'm a big fan of Test-driven development and have been using JUnit for 6 or 7 years (since around v3.4 or v3.5). I've written lots of mock objects (from simplistic dummy concrete classes that implement an entire interface, to Proxy-based mocks, to a pretty decent mock JMS library) and played with various mock libraries, but the one I'm sticking with and using almost exclusively is EasyMock.

    Reinventing a wheel is great for learning, and I've done it a lot. But perhaps in a sign that I'm getting older, I'm more concerned with getting stuff done, so now I tend to lean towards existing solutions when good ones exist. I searched for what people tend to use for mock objects and jMock and EasyMock are popular. I prefer the method-based approach of EasyMock to the string-based approach of jMock, since code changes will cause the tests to break immediately, and the mocks will be refactored along with the rest of the code if I use the IDE's refactoring features.

    The general pattern with creating mocks for a tier of your application is that you're trusting that that tier is tested and works correctly, so it's appropriate to have mocks return hard-coded values. We're testing that the caller of the mocked interface does the right thing with correct (or incorrect) inputs, so mocks help us to focus on what's being tested in isolation.

    I'd always found it difficult to unit test Spring MVC controllers, since it seems like they need a running container, and I don't want to deal with the hassle of setting up Cactus. Plus it seems somewhat useless to test controllers, since they shouldn't be doing much that isn't tested in proper automated functional tests. If they do too much business logic, they should be refactored to push that work out to the services layer, right?

    The reality is that we do too much business logic in controllers, and usually it's lazyness but often it's being pragmatic. We also tend to often do even more "illegal" activities in controllers, such as writing directly to the response rather than delegating to a View. This is usually the case for XML and JSON generated for Ajax responses. So until these get refactored, they need to be tested.

    It's helpful to have partially-functioning servlet container classes such as HttpServletRequest and HttpServletResponse, and the Spring Mock library (spring-mock.jar) is great for that. But for creating mocks for DAOs, services, and other interface-based dependency-injected resources, EasyMock greatly simplifies things. And with their user-contributed Class Extension, you can even mock out concrete classes.

    The syntax takes a while to get used to. Basically the flow that I tend to use is:

    1. Create a mock
    2. call expect(mock.[method call]).andReturn([result]) for each expected call
    3. call mock.[method call], then EasyMock.expectLastCall() for each expected void call
    4. call replay(mock) to switch from "record" mode to "playback" mode
    5. inject the mock as needed
    6. call the test method
    7. call verify(mock) to assure that all expected calls happened

    As simple as that may sound, it can look very weird at first. Say I have an interface:

    JAVA:
    1. public interface Thing {
    2.    void doSomething(String parameter);
    3.    int doSomethingElse(String parameter, int otherParameter);
    4. }

    I can create a mock for it via:

    JAVA:
    1. Thing thing = EasyMock.createMock(Thing.class);

    Then I can register expected calls via

    JAVA:
    1. EasyMock.expect(thing.doSomethingElse("woot", 5)).andReturn(123);
    2. EasyMock.expect(thing.doSomethingElse("fubar", 45)).andReturn(321);
    3.  
    4. thing.doSomething("p");
    5. EasyMock.expectLastCall();

    This says that in my calling code, when the client calls thing.doSomethingElse("woot", 5) to return 123, when the client calls thing.doSomethingElse("fubar", 45) to return 321, and to expect a call to thing.doSomething("p").

    I can then inject this mock into the class being tested in place of the real one, trusting that it will return known results. I can then concentrate on assuring that the tested class does the right thing.

    Thanks to EasyMock, my productivity for testing services and controllers is way up - and my code coverage percentages are too.

    This entry was posted on Monday, August 13th, 2007 at 2:43am and is filed under easymock, java, junit. You can follow any responses to this entry through the RSS 2.0 feed. You can skip to the end and leave a response. Pinging is currently not allowed.


    Source from http://burtbeckwith.com/blog/?p=43

    posted on 2010-04-21 18:23 lau 閱讀(311) 評論(0)  編輯  收藏 所屬分類: Unit Test


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


    網(wǎng)站導(dǎo)航:
     
    主站蜘蛛池模板: 亚洲福利视频一区二区| 成人免费777777| 亚洲AV无码成人专区片在线观看| 蜜芽亚洲av无码一区二区三区| 成人永久免费高清| 亚洲精品国产摄像头| 国产成人免费片在线视频观看| 亚洲AV无码一区二区一二区 | 亚洲国产人成在线观看| 日韩精品免费一级视频| 亚洲嫩草影院在线观看| 久久精品免费一区二区喷潮| 亚洲色大成网站www尤物| 免费a级毛片大学生免费观看| 窝窝影视午夜看片免费| 久久亚洲综合色一区二区三区| 国产精品免费无遮挡无码永久视频| 久久精品国产亚洲av麻豆色欲| 免费国产作爱视频网站| 亚洲a∨国产av综合av下载| 亚洲精品国产福利一二区| 成人片黄网站色大片免费观看APP| 亚洲国产精品国自产电影| 欧美在线看片A免费观看| 黄色免费网站在线看| 亚洲AV无码码潮喷在线观看 | 精品免费国产一区二区三区 | 一道本不卡免费视频| 国产亚洲一区二区三区在线| 8x8x华人永久免费视频| 亚洲国产成人综合精品| 亚洲色自偷自拍另类小说 | 亚洲成av人片不卡无码久久| 免费观看男人吊女人视频| 伊人久久五月丁香综合中文亚洲 | 中国china体内裑精亚洲日本| heyzo亚洲精品日韩| 精品免费tv久久久久久久| 亚洲私人无码综合久久网| 中文字幕亚洲不卡在线亚瑟| 性xxxxx免费视频播放|