I have a private method in my java code which needs to be invoked twice. I am writing junits using Powermock. While mocking the said method I need two different results for two different invokations.
//I have tried:
PowerMockito.doReturn("MyString1").doReturn("MyString2").when(spy,"getresult",Mockito.anyString());
//But when() is not accepting anything else than the spy object.
PowerMockito.doReturn("MyString1").doReturn("MyString2").when(spy).getresult(Mockito.anyString());
//when() is not letting to use the method getresult as this getresult method is declared private.
CODE
Class A{
String firstString="abc";
String secondString="def";
String result1=getresult(firstString);
String result2=getresult(secondString);
private String getresult(String arg1){
//My code here;
return "AnyString";
}
}
JUNIT
//Declarations
@InjectMocks
A a;
.......
@Test
public void ATest(){
....
/*Suppose I want "MyString1" and "MyString2" as result for calling the method "getresult" for result1 and result2 in Class A*/
A spy=PowerMockito.spy(a);
PowerMockito.doReturn("MyString1").when(spy,"getresult",Mockito.anyString());
....
}
// Please overlook the typos
I am getting compilation error while using the code that I tried. As written in the comments of code, I am expecting two results "MyString1" and "MyString2" in the two successive method calls as shown in the code.
Thanks in advance. Any other approach to achieve the results is appreciated.