I am mocking a static method as follows:
class A{
static String methodA(HashMap<String,Boolean> h){
...
Set<String> keys=h.entrySet();
}
}
Powermockito code:
Powermockito.mockstatic(A.class);
when(A.methodA(any(HashMap.class)).thenReturn("Hey");
Now, when I do not expect a call to methodA to enter the function definiton, but directly return “hey”. However, this is not happening. Despite the mock, the call to methodA() enters it, and since I am passing any(HashMap.class), a null value is passed. I get a NullPointerException. What am I doing wrong?
You need to have following on top of your class:
The annotation @RunWith, indicates that PowerMockRunner is to be used for executing the test case. Any class which requires static or private methods to be mocked, goes into @PrepareForTest, in this case : class A .