Mockito bypass static method for testing

后端 未结 3 1848
北海茫月
北海茫月 2020-12-17 16:15

I need to test handleIn() method using Mockito.

However the code need to call this legacy code Util.getContextPDO which is a static method.

Note that in test

3条回答
  •  一生所求
    2020-12-17 16:55

    A good technique for getting rid of static calls on 3rd party API is hiding the static call behind an interface.

    Let's say you make this interface :

    interface IPDOFacade {
    
        IPDO getContextPDO();
    }
    

    and have a default implementation that simply calls the static method on the 3rd party API :

    class IPDOFacadeImpl implements IPDOFacade {
    
        @Override
        public IPDO getContextPDO() {
            return Util.getContextPDO();
        }
    }
    

    Then it is simply a matter of injecting a dependency on the interface into MyClass and using the interface, rather than the 3rd party API directly :

    public class MyClass {
    
        private final IPDOFacade ipdoFacade;
    
        public MyClass(IPDOFacade ipdoFacade) {
            this.ipdoFacade = ipdoFacade;
        }
    
        public String handleIn(Object input) throws Throwable
        {
            String result = "";
            IPDO pdo = getIPDO();
    
            someImportantBusinessLogic(pdo);
    
            return result;
        }
    
        ...
    
    }
    

    In your unit test, you can then easily mock your own interface, stub it any way you like and inject it into the unit under test.

    This

    • avoids the need to make private methods package private.
    • makes your tests more readable by avoiding partial mocking.
    • applies inversion of control.
    • decouples your application from a specific 3rd party library.

提交回复
热议问题