我正在尝试找一篇关于模仿私有方法的文章。实际上我们在我们的项目中使用了mockito,但它的测试覆盖率非常差。所以我试着用Mockito和PowerMock写作,但我找不到任何好的例子等等。任何人都能解释一下吗?
答案 0 :(得分:3)
我认为这是PowerMock的工作。我怀疑Mockito可以做到这一点。 The PowerMock documentation explains how to do it.并且,它以此为例:
@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);
}