如何使用PowerMock模拟私有方法进行测试



我有一个类,我想用一个调用私有方法的公共方法来测试。我想假设私有方法工作正确。例如,我想要doReturn....when...。我发现使用PowerMock有一个可能的解决方案,但是这个解决方案不适合我。怎样才能做到呢?有人遇到这个问题吗?

我看不出有什么问题。通过使用Mockito API的以下代码,我成功地做到了这一点:

public class CodeWithPrivateMethod {
    public void meaningfulPublicApi() {
        if (doTheGamble("Whatever", 1 << 3)) {
            throw new RuntimeException("boom");
        }
    }
    private boolean doTheGamble(String whatever, int binary) {
        Random random = new Random(System.nanoTime());
        boolean gamble = random.nextBoolean();
        return gamble;
    }
}
下面是JUnit测试:
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import static org.mockito.Matchers.anyInt;
import static org.mockito.Matchers.anyString;
import static org.powermock.api.mockito.PowerMockito.when;
import static org.powermock.api.support.membermodification.MemberMatcher.method;
@RunWith(PowerMockRunner.class)
@PrepareForTest(CodeWithPrivateMethod.class)
public class CodeWithPrivateMethodTest {
    @Test(expected = RuntimeException.class)
    public void when_gambling_is_true_then_always_explode() throws Exception {
        CodeWithPrivateMethod spy = PowerMockito.spy(new CodeWithPrivateMethod());
        when(spy, method(CodeWithPrivateMethod.class, "doTheGamble", String.class, int.class))
                .withArguments(anyString(), anyInt())
                .thenReturn(true);
        spy.meaningfulPublicApi();
    }
}

对于任何测试框架(如果您的类是非final),一个通用的解决方案是手动创建您自己的mock。

  1. 将您的私有方法更改为protected
  2. 在你的测试类中扩展类
  3. 重写之前私有的方法来返回任何你想要的常量

这没有使用任何框架,所以它不那么优雅,但它总是工作的:即使没有PowerMock。或者,您可以使用Mockito执行步骤#2 &第三步,如果你已经完成了第一步。

要直接模拟私有方法,需要使用PowerMock,如另一个答案所示。

出于某种原因,Brice的回答对我不起作用。我可以稍微操纵一下,让它工作。这可能只是因为我有一个新版本的PowerMock。我用的是1.6.5。

import java.util.Random;
public class CodeWithPrivateMethod {
    public void meaningfulPublicApi() {
        if (doTheGamble("Whatever", 1 << 3)) {
            throw new RuntimeException("boom");
        }
    }
    private boolean doTheGamble(String whatever, int binary) {
        Random random = new Random(System.nanoTime());
        boolean gamble = random.nextBoolean();
        return gamble;
    }
}

测试类如下:

import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import static org.mockito.Matchers.anyInt;
import static org.mockito.Matchers.anyString;
import static org.powermock.api.mockito.PowerMockito.doReturn;
@RunWith(PowerMockRunner.class)
@PrepareForTest(CodeWithPrivateMethod.class)
public class CodeWithPrivateMethodTest {
    private CodeWithPrivateMethod classToTest;
    @Test(expected = RuntimeException.class)
    public void when_gambling_is_true_then_always_explode() throws Exception {
        classToTest = PowerMockito.spy(classToTest);
        doReturn(true).when(classToTest, "doTheGamble", anyString(), anyInt());
        classToTest.meaningfulPublicApi();
    }
}

我知道一种方法,你可以调用你的私有函数在模拟测试

@Test
    public  void  commandEndHandlerTest() throws  Exception
    {
        Method retryClientDetail_privateMethod =yourclass.class.getDeclaredMethod("Your_function_name",null);
        retryClientDetail_privateMethod.setAccessible(true);
        retryClientDetail_privateMethod.invoke(yourclass.class, null);
    }

不带参数:

ourObject = PowerMockito.spy(new OurClass());
when(ourObject , "ourPrivateMethodName").thenReturn("mocked result");

String参数:

ourObject = PowerMockito.spy(new OurClass());
when(ourObject, method(OurClass.class, "ourPrivateMethodName", String.class))
                .withArguments(anyString()).thenReturn("mocked result");

一些考虑

确保私有函数正在调用另一个公共函数,并且您只能继续模拟公共函数。

相关内容

  • 没有找到相关文章

最新更新