PowerMockito和静态方法

时间:2016-11-10 19:20:44

标签: unit-testing junit mockito powermock powermockito

我正在尝试使用一堆静态方法测试实用程序类。我正在为一个调用另外两个静态方法的方法编写测试。在第一个静态方法调用模拟似乎工作但它忽略了第二个期望并调用真正的方法。任何的想法?我尝试在第二次期望之前提供第二个PowerMock.spy(Utils.class),但没有运气。知道怎么解决这个问题吗?

这是示例代码。

    package com.personal.test;

import java.io.IOException;

public class Utils {

    public static String testUtilOne() {
        System.out.println("Static method one");
        return "methodone";
    }

    public static void testUtilsTwo(String s1, String s2, String s3) throws IOException {
        throw new IOException("Throw an exception");
    }

    public static void testUtilsThree() throws TestException {
        try {
            String s = testUtilOne();
            testUtilsTwo("s1", s, "s3");
        } catch (IOException ex) {
            throw new TestException("there was an exception", ex);
        }
    }

    public static class TestException extends Exception {
        public TestException() {
            super();
        }
        public TestException(String message, Exception cause) {
            super(message, cause);
        }
    }
}


    package com.personal.test;

import java.io.IOException;

import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;

import com.personal.test.Utils.TestException;

@RunWith(PowerMockRunner.class)
@PrepareForTest({Utils.class})
public class UtilsTest {

    @Test
    public void testMethodThreeWrapsException() throws Exception {
        PowerMockito.spy(Utils.class);
        PowerMockito.when(Utils.class, "testUtilOne").thenReturn("This is coming from test");
        PowerMockito.spy(Utils.class);
        PowerMockito.when(Utils.class, "testUtilsTwo", Mockito.anyString(), Mockito.anyString(), Mockito.anyString()).thenThrow(new IOException("Exception from test"));
        try {
            Utils.testUtilsThree();
        } catch (TestException ex) {
            Assert.assertTrue(ex.getCause() instanceof IOException);
            Assert.assertEquals("Exception from test", ex.getMessage());
        }
    }
}

1 个答案:

答案 0 :(得分:1)

如果有任何人感兴趣,请等待。在花了一整天的时间尝试后我终于解决了这个问题。我首先使用staticMocked我的Utils类(这将模拟所有静态方法),而不是要求mock为testUtilsThree调用调用实际方法。 变更如下。

        package com.personal.test;

import java.io.IOException;

import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;

import com.personal.test.Utils.TestException;

@RunWith(PowerMockRunner.class)
@PrepareForTest({Utils.class})
public class UtilsTest {

    @Test
    public void testMethodThreeWrapsException() throws Exception {
        PowerMockito.mockStatic(Utils.class);
        PowerMockito.when(Utils.class, "testUtilOne").thenReturn("This is coming from test");
        PowerMockito.when(Utils.class, "testUtilsTwo", Mockito.anyString(), Mockito.anyString(), Mockito.anyString()).thenThrow(new IOException("Exception from test"));
        PowerMockito.doCallRealMethod().when(Utils.class, "testUtilsThree");
        try {
            Utils.testUtilsThree();
        } catch (TestException ex) {
            Assert.assertTrue(ex.getCause() instanceof IOException);
            Assert.assertEquals("Exception from test", ex.getMessage());
        }
    }
}