0

我正在尝试查找有关模拟私有方法的文章。实际上我们在项目中使用了 mockito,但它的测试覆盖率太差了。所以我试着用 Mockito 和 PowerMock 写,但我找不到任何好的例子等等。有人可以解释一下吗?

4

1 回答 1

3

我认为这是 PowerMock 的工作。我怀疑 Mockito 能做到。 PowerMock 文档解释了如何做到这一点。 并且,它以此为例:

@Test
public void testReplaceData() throws Exception {
        final String modifyDataMethodName = "modifyData";
        final byte[] expectedBinaryData = new byte[] { 42 };
        final String expectedDataId = "id";

        // Mock only the modifyData method
        DataService tested = createPartialMock(DataService.class, modifyDataMethodName);

        // Expect the private method call to "modifyData"
        expectPrivate(tested, modifyDataMethodName, expectedDataId,
                        expectedBinaryData).andReturn(true);

        replay(tested);

        assertTrue(tested.replaceData(expectedDataId, expectedBinaryData));

        verify(tested);
}
于 2013-06-18T00:24:54.797 回答