我需要使用Mockito测试handleIn()方法.
但是代码需要调用这个遗留代码Util.getContextPDO,这是一个静态方法.
请注意,在测试环境中,此Util.getContextPDO始终返回Exception,并且我打算通过始终返回虚拟IPDO来绕过此Util.getContextPDO().
public class MyClass {
public IPDO getIPDO()
{
return Util.getContextPDO(); // note that Util.getContextPDO() is a static, not mockable.
}
public String handleIn(Object input) throws Throwable
{
String result = "";
IPDO pdo = getIPDO();
// some important business logic.
return result;
}
}
最初我认为这可以通过使用类“MyClass”的spy()来实现,所以我可以模拟getIPDO()的返回值.以下是我使用spy()的初步努力
@Test
public void testHandleIn() throws Exception
{
IPDO pdo = new PDODummy();
MyClass handler = new MyClass ();
MyClass handler2 = spy(handler);
when(handler2.getIPDO()).thenR