I am having some trouble trying to unit test java code that at some point calls native methods. Basically, I am trying to use PowerMockito
to mock the class that will eventually call native. I was able to mock non-void methods just fine, but I keep getting compilation errors when I try to mock a method of void return type. Here is an example of the code I'm trying to test:
public class ClassThatCallsNative {
void initObject(ByteBuffer param1, int param2) {
//calls native
}
int getId(int param1) {
//calls native
}
}
I have this code in my test class:
PowerMockito.when(mClassThatCallsNative.getId(Mockit.anyInt())).thenReturn(0);
This line of code compiles just fine, however the following line is where I get compilation error:
PowerMockito.when(mClassThatCallsNative.initObject(Mockit.any(ByteBuffer.class), anyInt())).doNothing();
The error message just says invalid void parameter and points to .initObject. Any idea what I am doing wrong?
Since you are trying to mock method which returns void, you simply can't call it inside when() method. This is because PowerMockito.when() methods expects T methodCall but got void, this is the reason for compilation failure. Instead you should use this syntax:
PowerMockito.doNothing().when(mClassThatCallsNative).initObject(any(ByteBuffer.class), anyInt())
any() and anyInt() methods are part of Mockito class.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With