I am using Junit4 and Mockito for writing my test cases. In one of the classes which is under test, there is a function init(), which gets called from the constructor.
void init(){
//Some code
Handler handler = new Handler(Looper.getMainLooper());
handler.post(new Runnable() {
@Override
public void run() {
//Some code
}
});
}
The following exception is thrown when trying to create constructor of that class.
java.lang.RuntimeException: Method post in android.os.Handler not mocked.
Then I tried to mock post method of the Handler class using the following code
Handler handler = spy(new Handler());
when(handler.post(Matchers.any(Runnable.class))).thenReturn(true);
But still I keep on getting the same exception. What should I do to stub the post method of the Handler class ?