我想模拟一个静态方法以及一个类的非静态方法。我的来源看起来像:
public class XDSUtilityManager
{
private static XDSUtilityManager xdsUtilMgr = new XDSUtilityManager();
private XDSUtilityManager()
{
xdsUtilMgrImpl = new XDSUtilityManagerImpl();
}
public static XDSUtilityManager getInstance()
{
return (xdsUtilMgr == null ) ? new XDSUtilityManager() : xdsUtilMgr;
}
public XMLDocument getXMLDocument(final String absoluteKey, final XDSClient xdsClient)
{
return getXMLDocument(absoluteKey, xdsClient, false);
}
}
我想模拟静态方法getInstance()。我希望 getInstance() 返回XDSUtilityManager类的模拟对象。我还想模拟不是静态的getXMLDocument() 。
在我的测试用例中,我尝试了以下操作:
XMLDocument xmlDocument = PowerMock.createMock(XMLDocument.class);
XDSUtilityManager xdsUtilityManager = PowerMock.createPartialMock(XDSUtilityManager.class,"getXMLDocument");
PowerMock.mockStaticPartial(XDSUtilityManager.class, "getInstance");
expect(XDSUtilityManager.getInstance()).andReturn(xdsUtilityManager).anyTimes();
expect(xdsUtilityManager.getXMLDocument((String)anyObject(), anyObject(XDSClient.class))).andReturn(xmlDocument).anyTimes();
PowerMock.replay(xdsUtilityManager);
PowerMock.replay(xmlDocument);
但事情并没有按预期工作。请帮忙