测试(模拟)一个void函数,它在内部调用与数据库

时间:2016-04-20 11:03:34

标签: java unit-testing mocking mockito powermockito

我有一个类,其中有一个私有变量Connection。我想测试functionA,我必须模拟functionB和functionC。

我尝试使用powermock和mockito但不能这样做。

测试fucntionA和模拟functionB和functionC应该做些什么。

public class ToMock {

    private Connection connection;

    private static functionA(String name) {       
        // do something    
        functionB()    
        return xyz;
    }

    public static void functionB() {
         connection = functionC("localhost", 10000);    
    }

    public static void functionC(String hostName, int port) {
        //make a connection to db

        String connectionString = String.format("jdbc:hive2://%s:%d/",emrHost, port);

        LOGGER.info("Connection string {}", connectionString);

        try {

            Class.forName("org.apache.hive.jdbc.HiveDriver");    
            Connection con = DriverManager.getConnection(connectionString, "hadoop", "");

            LOGGER.info("Connected successfully");

            return con;    
        } catch (ClassNotFoundException e) {
            throw Throwables.propagate(e);
        }
    }
}

2 个答案:

答案 0 :(得分:0)

你有static个方法。因此,您不能通过其设计使用Mockito。你可以使用PowerMock。 请参阅此处的用法:Link

见这里:link

基本上看起来像这样的代码(从PowerMock示例中复制),

@RunWith(PowerMockRunner.class)
@PrepareForTest(Static.class)
public class YourTestCase {
@Test
public void testMethodThatCallsStaticMethod() {
    // mock all the static methods in a class called "Static"
    PowerMockito.mockStatic(Static.class);
    // use Mockito to set up your expectation
    Mockito.when(Static.firstStaticMethod(param)).thenReturn(value);
    Mockito.when(Static.secondStaticMethod()).thenReturn(123);

    // execute your test
    classCallStaticMethodObj.execute();

    // Different from Mockito, always use PowerMockito.verifyStatic() first
    // to start verifying behavior
    PowerMockito.verifyStatic(Mockito.times(2));
    // IMPORTANT:  Call the static method you want to verify
    Static.firstStaticMethod(param);


    // IMPORTANT: You need to call verifyStatic() per method verification, 
    // so call verifyStatic() again
    PowerMockito.verifyStatic(); // default times is once
    // Again call the static method which is being verified 
    Static.secondStaticMethod();

    // Again, remember to call verifyStatic()
    PowerMockito.verifyStatic(Mockito.never());
    // And again call the static method. 
    Static.thirdStaticMethod();
}
}

这里Static是静态方法所属的类。

答案 1 :(得分:0)

像@ neurotic-d描述的那样重构你的代码。像这样:

public class ToMock {

    private Connection connection;

    public ToMock(Connection connection){
        this.connection = connection;
    }

    private functionA(String name) {       
        // do something    

        return xyz;
    }
}  



public class ToMockFactory {

    public static ToMock toMock(){
        return new ToMock(functionB());
    }

    public static Connection functionB() {
        return functionC("localhost", 10000);    
    }

    public static Connection functionC(String hostName, int port) {
        //make a connection to db

        String connectionString =     String.format("jdbc:hive2://%s:%d/",emrHost, port);

        LOGGER.info("Connection string {}", connectionString);

        try {

            Class.forName("org.apache.hive.jdbc.HiveDriver");    
            Connection con = DriverManager.getConnection(connectionString,   "hadoop", "");

            LOGGER.info("Connected successfully");

            return con;    
        } catch (ClassNotFoundException e) {
            throw Throwables.propagate(e);
        }
    }
}