问题
java.lang.RuntimeException:未模拟 android.os.CountDownTimer 中的方法启动。
尽管我已经模拟过了。
Here is my code
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
}
最佳答案
将此行添加到模块 build.gradle
android {
...
testOptions {
unitTests.returnDefaultValues = true
} }
这段代码解决了我的问题
关于内容为CountDownTimer类的Android单元测试方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42598039/