powermock
powermock copied to clipboard
failure to mock CountDownTimer class
Problem
java.lang.RuntimeException: Method start in android.os.CountDownTimer not mocked.
even though that i have mock it already.
Here is my code
@RunWith(PowerMockRunner.class)
@PrepareForTest(someClass.class)
void mSomething() {
new CountDownTimer(5000, 5000) {
public void onTick(long millisUntilFinished) {
}
public void onFinish() {
//do somethig
}
}.start();
}
Here is my Test
@Test
public void testSomething() throws Exception {
CountDownTimer countDownTimer = mock(CountDownTimer.class);
PowerMockito.whenNew(CountDownTimer.class).withAnyArguments().thenReturn(countDownTimer);
someClass.mSomething();
//verify
}
add this line to module build.gradle
android {
...
testOptions {
unitTests.returnDefaultValues = true
} }
add this line to module
build.gradleandroid { ... testOptions { unitTests.returnDefaultValues = true } }
It worked for me.