Reputation: 7312
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?
Upvotes: 1
Views: 4152
Reputation: 4112
You need to have following on top of your class:
@RunWith(PowerMockRunner.class)
@PrepareForTest({ A.class})
class ATest {
....
}
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 .
Upvotes: 4