Reputation: 529
I want to mock a static method and also non-static methods of a class. My source looks like:
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);
}
}
I want to mock static method getInstance(). I want getInstance() to return mock object of XDSUtilityManager class. Also I want to mock getXMLDocument() which is not static.
And in my testCase I tried following:
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);
But things are not working as expected. Please help
Upvotes: 1
Views: 14751
Reputation: 17085
Its not required to call getInstance()
method to create the instance of XDSUtilityManager
.
Using PowerMockito
, as your constructor is private you need to suppress the constructor
before you call create mock as below..
PowerMockito.suppress(PowerMockito.constructor(XDSUtilityManager.class));
// enable static mocking for all the methods in the class
PowerMockito.mockStatic(XDSUtilityManager.class);
XDSUtilityManager xDSUtilityManager = PowerMockito.mock(XDSUtilityManager.class);
XMLDocument xmlDocument = PowerMock.createMock(XMLDocument.class);
PowerMockito.when(xDSUtilityManager.getXMLDocument(
Mockito.anyString(),Mockito.anyObject())).thenReturn(xmlDocument);
Upvotes: 1
Reputation: 4010
The easiest way I have found to do this is by using PowerMockito. PowerMockito is a combination of Mockito and PowerMock, which allows the mocking of static objects.
The pattern I have used is to use the mock your static getInstance()
to return a copy of your non static mock, which you then extend as normal. An example using PowerMockito would be:
@RunWith(PowerMockRunner.class)
@PrepareForTest({SingletonObject.class})
public class SingletonTester {
@Mock
private SingletonObject singleMock;
@Before
public void setup(){
// initialize all the @Mock objects
MockitoAnnotations.initMocks(this);
// mock all the statics
PowerMockito.mockStatic(SingletonObject.class);
}
@Test
public void mockTester(){
// Mock the static getInstance call to return the non-Static Mock
Mockito.when(SingletonObject.getInstance()).thenReturn(singleMock);
// Mock the non static version as normal
PowerMockito.when(singleMock.nonStaticMethodCall()).thenReturn("Whatever you need.");
//..........
}
}
Your static call to getInstance()
to get the singleton object returns the instantiated mock object you ALSO define. Once you tell the static what to return, you can continue to mock the non-static calls as normal.
Upvotes: 7