@Test
public void testnonVoidPublicStaticMethod() throws Exception
{
//It Mocks the static methods of the final class Helper
PowerMockito.mockStatic(Helper.class);
/**
* The Behaviour defining section
*/
/**
* Type 1
*
* In this version the parameters could be defined with Mockito
* Matchers Like anyObject(), anyInt() etc...
*
* The return value can also be specified as an Answer implementation
* with the help of API's like PowerMockito.doAnswer
* (<< An answer implementation >>) like answerIsArgumentString() as provided below.
*
* the doCallRealMethod() returns the real Method implementation
*
*/
//PowerMockito.doReturn("ok").when(Helper.class, "nonVoidPublicStaticMethod",
anyObject(),anyObject());
//PowerMockito.doCallRealMethod().when(Helper.class, "nonVoidPublicStaticMethod",
anyObject(),anyObject());
//PowerMockito.doAnswer(answerIsArgumentDate()).when(Helper.class, "nonVoidPublicStaticMethod",
any(),any());
/**
* Type 2
*
* In this version the parameters to be defined as original objects to be matched instead of Matchers..
*
* The return value can also be specified as an Answer implementation with the help of
* API's like thenAnswer(<< An answer implementation >>) like answerIsArgumentDate()
* as provided below.
*
* the thenCallRealMethod() returns the real Method implementation
*
*/
/*when(Helper.nonVoidPublicStaticMethod(entity,stringBuilder)).thenCallRealMethod();
when(Helper.nonVoidPublicStaticMethod(entity,stringBuilder)).thenReturn("ok");
*/
when(Helper.nonVoidPublicStaticMethod(entity,stringBuilder)).thenAnswer(answerIsArgumentString());
/**
* End of Behaviour defined section
*
*/
String ok = Helper.nonVoidPublicStaticMethod(entity,stringBuilder.append("ok Subhankar"));
//System.out.println("The nonVoidPublicStaticMethod Result is:" +ok);
}
//This method returns the toString() version of the second argument of the method invoked
private static Answer answerIsArgumentString() {
return new Answer() {
public String answer(InvocationOnMock invocation) {
String str = (invocation.getArguments()[1].toString());
return str;
}
};
}
//This method returns the toString() version of the first argument of the method invoked
private static Answer answerIsArgumentDate() {
return new Answer() {
public String answer(InvocationOnMock invocation) {
String str = (invocation.getArguments()[0]).toString();
return str;
}
};
}
Sunday, 22 March 2015
Mocking Non Void Public Static Method
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment