<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

    Mocking Static Calls

    How can you test methods that contain static method calls? This is a question we're facing at the moment while working on an application of which parts have been written by another development team. In order to gain insight into functionality and quality of the codebase, we are writing JUnit tests. But a lot of the code is dependent on either the JSF FacesContext or the Spring ApplicationContext being available.

    It is however impossible to mock static method calls like FacesContext.getCurrentInstance(). So how do we go around testing these calls out of container? We've thought up three different approaches, but each has its pros and cons. Let us first take a look at these three approaches...

    1. Extract method
    Introduce a method in each class that looks like the following:

     
    protected FacesContext getFacesContext() {
    return FacesContext.getCurrentInstance();
    }
     

    This can be easily mocked out in your test by creating the class with an overridden method, as follows:

     
    FacesContext mockContext = EasyMock.createMock(FacesContext.class);
    new MyObjectUnderTest() {
    protected FacesContext getFacesContext() {
    return mockContext;
    }
    }
     

    Pros

    • Easy to refactor using extract method

    Cons

    • Lots of code duplication

    2. Static Helper
    Our second option is to introduce a static helper class which has the following implementation:

     
    public class FacesContextHelper {
    private static FacesContext context;
     
    public static FacesContext getCurrentFacesContext() {
    return context != null ? context : FacesContext.getCurrentInstance();
    }
     
    public static void setFacesContext(FacesContext facesContext) {
    context = facesContext;
    }
     
    public static void reset() {
    context = null;
    }
    }
     

    Now in a testclass we can just write down the following line to mock out the FacesContext:

     
    FacesContextHelper.setFacesContext(EasyMock.createMock(FacesContext.class));
     

    Pros

    • No real painful code-duplication
    • Easy to do search-replace to convert all calls to FacesContext.getCurrentInstance() to FacesContextHelper.getCurrentFacesContext().

    Cons

    • Never forget to call reset() in the teardown of your test to prevent another test from picking up your mock.
    • Doesn't feel right to write static setter methods in your code just to enable testing it. Not an OO approach?

    3. OO Helper class
    Our third and last option is to introduce an interface and implementation of the FacesContextHelper:

     
    public interface FacesContextHelper {
    FacesContext getCurrentFacesContext();
    }
     
    public class FacesContextHelperImpl implements FacesContextHelper {
    public FacesContext getCurrentFacesContext() {
    return FacesContext.getCurrentInstance();
    }
    }
     

    We now need to introduce an instance of the FacesContextHelperImpl to each class. For this example, we will use a package protected variable in each class. It is of course also possible to use a setter method. For our test cases we now need to introduce a new FacesContextHelper:

     
    public class MockFacesContextHelper implements FacesContextHelper {
    private FacesContext mockContext;
     
    public MockFacesContextHelper(FacesContext mockContext) {
    this.mockContext = mockContext;
    }
     
    public FacesContext getCurrentFacesContext() {
    return this.mockContext;
    }
    }
     

    In our test cases we can now easily mock out the FacesContext again by setting the package protected field:

     
    someClazz.facesContextHelper = new MockFacesContextHelper(EasyMock.createMock(FacesContext.class));
     

    Pros

    • Feels more natural and OO than the static helper solution

    Cons

    • Need to introduce a new field to each and every class which needs the FacesContext mocked out.

    That's it. I myself am still doubting as to which method is the lesser evil, not one feels absolutely right. What do you think, which method do you consider better? Did I overlook another option perhaps?

    posted on 2010-02-25 09:12 lau 閱讀(346) 評論(0)  編輯  收藏 所屬分類: Unit Test

    主站蜘蛛池模板: 99热在线精品免费全部my| 999任你躁在线精品免费不卡| 三年片在线观看免费大全| 久久亚洲AV无码精品色午夜麻豆 | 久久亚洲免费视频| 久久亚洲AV无码精品色午夜麻| 久久久久久影院久久久久免费精品国产小说 | 色欲色欲天天天www亚洲伊| 在线观看无码的免费网站| 亚洲 暴爽 AV人人爽日日碰| 丁香花免费高清视频完整版| 亚洲色大成网站www久久九| 国产精品免费视频一区| 男女污污污超污视频免费在线看 | 亚洲国产成人片在线观看| 日韩免费电影网站| 亚洲最大免费视频网| 四虎国产精品免费久久| 女bbbbxxxx另类亚洲| 久久亚洲国产成人精品无码区| 国产一精品一AV一免费| 亚洲天堂福利视频| 国产中文字幕免费| 中文字幕在线免费看线人| 亚洲网站在线播放| 国产男女性潮高清免费网站| 一级毛片免费在线| 亚洲综合区图片小说区| 日本大片在线看黄a∨免费| 一级特黄a大片免费| 亚洲精品国产电影午夜| 夜色阁亚洲一区二区三区| 日本道免费精品一区二区| 亚洲乱人伦精品图片| 中文字幕第13亚洲另类| 久草免费在线观看视频| 成年免费大片黄在线观看com| 亚洲男人都懂得羞羞网站| 国产精品麻豆免费版| 久久精品成人免费看| 亚洲精品精华液一区二区|