powermock icon indicating copy to clipboard operation
powermock copied to clipboard

failure to mock CountDownTimer class

Open iasmar opened this issue 8 years ago • 2 comments

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
}

iasmar avatar Mar 05 '17 08:03 iasmar

add this line to module build.gradle

android {
    ...
    testOptions {
        unitTests.returnDefaultValues = true
    } }

codemaker2015 avatar Dec 05 '21 07:12 codemaker2015

add this line to module build.gradle

android {
    ...
    testOptions {
        unitTests.returnDefaultValues = true
    } }

It worked for me.

SurajBahadur avatar Feb 04 '22 19:02 SurajBahadur