问题描述
我正在编写 junit 测试来测试其中声明了类的私有方法。
我的代码:
private Object getFunctReturn()
{
InterfaceName obj = new ClassName():
Map map = obj.getMap();
map.foreach()->{
//iterating map here....
}
}
我正在使用 WhiteBox api 来测试私有方法。我如何模拟 InterfaceName obj = new ClassName();以便我可以继续执行下一次代码?
解决方法
试试这个:
@RunWith(PowerMockRunner.class)
@PrepareForTest(ClassUnderTest.class)
public class SimpleTest {
@Test
public void test() throws Exception {
ClassUnderTest underTest = new ClassUnderTest();
ClassName obj = new ClassName();
PowerMockito.whenNew(ClassName.class).withNoArguments().thenReturn(obj);
WhiteboxImpl.invokeMethod(underTest,"getFunctReturn");
}
}