@Test
public void testnonVoidPrivateMethod() throws Exception
{
Helper helper = PowerMockito.spy(new Helper());
/**
* Please Note that the PowerMockito.spy also takes the class type as
* an Instance and then its return type is void i.e. in this case no
* Mocked instance is available.
*/
/**
* Both the behaviours defined below are same,
* only one returns Answer implementation and other
* returns Normal Object
*
*/
//doAnswer(answerIsArgumentString()).when(helper,method
(Helper.class,"nonVoidPrivateMethod",entity.class,StringBuilder.class))
.withArguments(entity, stringBuilder);
doReturn("OKuuui").when(helper,method(Helper.class,"nonVoidPrivateMethod",Entity.class,
StringBuilder.class)).withArguments(anyObject(), anyObject());
/**
* This call will make the original method call Since, we are not
* calling with the mocked instance
*/
//String ok = new Helper().callNonVoidPrivateMethod(entity,stringBuilder.append("ok Subhankar"));
/*
* Now if we do invoke with the mocked instance create above we would get mocked behaviour
*
*/
String ok = helper.callNonVoidPrivateMethod(entity,stringBuilder.append("ok Subhankar"));
System.out.println("The nonVoidPrivateMethod 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 private Method
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment