Mocking static methods with Mockito

后端 未结 15 1361
Happy的楠姐
Happy的楠姐 2020-11-21 06:51

I\'ve written a factory to produce java.sql.Connection objects:

public class MySQLDatabaseConnectionFactory implements DatabaseConnectionFactory         


        
15条回答
  •  星月不相逢
    2020-11-21 07:28

    The typical strategy for dodging static methods that you have no way of avoiding using, is by creating wrapped objects and using the wrapper objects instead.

    The wrapper objects become facades to the real static classes, and you do not test those.

    A wrapper object could be something like

    public class Slf4jMdcWrapper {
        public static final Slf4jMdcWrapper SINGLETON = new Slf4jMdcWrapper();
    
        public String myApisToTheSaticMethodsInSlf4jMdcStaticUtilityClass() {
            return MDC.getWhateverIWant();
        }
    }
    

    Finally, your class under test can use this singleton object by, for example, having a default constructor for real life use:

    public class SomeClassUnderTest {
        final Slf4jMdcWrapper myMockableObject;
    
        /** constructor used by CDI or whatever real life use case */
        public myClassUnderTestContructor() {
            this.myMockableObject = Slf4jMdcWrapper.SINGLETON;
        }
    
        /** constructor used in tests*/
        myClassUnderTestContructor(Slf4jMdcWrapper myMock) {
            this.myMockableObject = myMock;
        }
    }
    

    And here you have a class that can easily be tested, because you do not directly use a class with static methods.

    If you are using CDI and can make use of the @Inject annotation then it is even easier. Just make your Wrapper bean @ApplicationScoped, get that thing injected as a collaborator (you do not even need messy constructors for testing), and go on with the mocking.

提交回复
热议问题