【问题标题】:How to mock private method for testing using PowerMock?如何使用 PowerMock 模拟私有方法进行测试?
【发布时间】:2011-12-09 21:02:02
【问题描述】:

我有一个类,我想用一个调用私有方法的公共方法对其进行测试。我想假设私有方法可以正常工作。例如,我想要doReturn....when... 之类的东西。我发现有possible solution using PowerMock,但是这个解决方案对我不起作用。 怎么做?有人遇到过这个问题吗?

【问题讨论】:

  • 另一种选择是保护私有方法并在您的测试用例中为其添加覆盖。
  • 一般来说,如果您需要存根私有方法,您的对象模型就有问题 - 您是否考虑过重构?
  • @Emma 为什么?如果他的方法调用了一些外部资源,比如数据库,并且他想模拟它以注入一些虚假结果怎么办?
  • @grinch 他应该在单独的适配器类中提取用于访问外部资源的代码。这样他就可以轻松地模拟适配器类,并将测试类中的(业务)逻辑与访问外部资源的技术细节分开。

标签: java testing junit mockito powermock


【解决方案1】:

我认为这里没有问题。通过使用 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();
    }
}

【讨论】:

  • 感谢您的回答,他们 wiki 上的示例代码仅显示了使用 EasyMock 后端的 API,而不是 Mockito。
  • 请注意其他在 IDE 内容辅助收藏夹中有 Hamcrest 匹配器类的开发人员:它们不适用于 Mockito 的 .withArguments() 方法 - 您必须使用 Mockito 匹配器! ;) 我花了一段时间才弄清楚为什么我的测试代码不断抛出异常。
  • @Brice 但是你如何管理来自“when(spy, method(.....”)的异常?让测试抛出异常好还是使用 try catch 好?
  • RuntimeException@Expected(...) 只是示例的一部分。在 JUnit 的过去几年中,我发现 try catch 是目前测试异常行为的最佳方法,至少在 Java 7 上是这样(参见 answer)。因为使用 Java 8 lambdas 可能会对此进行改进。
  • 这对我不起作用,但是来自另一个问题的答案确实如此:stackoverflow.com/a/41519820/3860594
【解决方案2】:

适用于任何测试框架的通用解决方案(如果您的类不是final)是手动创建您自己的模拟。

  1. 将您的私有方法更改为受保护。
  2. 在您的测试类中扩展类
  3. 覆盖以前的私有方法以返回您想要的任何常量

这不使用任何框架,因此它不那么优雅,但它总是可以工作:即使没有 PowerMock。或者,如果您已经完成了第 1 步,您可以使用 Mockito 为您执行第 2 步和第 3 步。

要直接模拟私有方法,您需要使用 PowerMock,如 other answer 所示。

【讨论】:

  • 这真是天才……谢谢。
  • @ArtB 如果将私有方法更改为受保护,则不再需要创建自己的模拟,因为受保护也可用于整个包。 (并且测试sohuld与要测试的类属于同一个包)。
  • @AnthonyRaymond 但是如果该方法正在执行您不想在测试中执行的操作怎么办?例如,我有一些遗留代码打开了一个硬编码的本地文件,该文件预计存在于生产环境中,但不存在于开发环境中;如果在开发中完成它会导致它死亡。我使用这种技术跳过了对该文件的读取。
  • @ArtB 我同意,但问题是“如何模拟私有方法”,这意味着该类将成为模拟。因此,如果该方法是可见性包,您可以根据您的期望模拟该方法。 IMO 扩展课程而不是嘲笑是用锤子砸苍蝇。
  • 谢谢,我尝试了多种解决方案,这太简单了,太棒了。很遗憾我没有马上想到同样的事情。
【解决方案3】:

出于某种原因,布莱斯的回答对我不起作用。我能够对其进行一些操作以使其正常工作。可能只是因为我有更新版本的 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();
    }
}

【讨论】:

  • 这听起来很明显,但我在导入 org.mockito.Mockito.doReturn 而不是 org.powermock.api.mockito.PowerMockito.doReturn 时遇到了麻烦。一旦我使用了正确的导入,@gaaogong 对我来说是一种享受
【解决方案4】:

我知道一种方法,你可以称你为私有函数在 mockito 中进行测试

@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);
    }

【讨论】:

    【解决方案5】:

    没有参数:

    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");
    

    【讨论】:

      【解决方案6】:

      需要考虑的事项

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

      【讨论】:

        猜你喜欢
        • 2022-01-23
        • 2018-10-19
        • 1970-01-01
        • 2014-10-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多