【发布时间】:2015-11-11 17:26:41
【问题描述】:
在提交错误报告之前,我想确保自己没有做任何严重错误的事情。这真的很奇怪。设置:
机器人电动 3.0;模拟 1.10.19
待测单元:
public BbScrollView( Context context ){
this( context, null );
}
public BbScrollView( Context context, AttributeSet attrs ) {
super( context, attrs );
mScrollTask = new Runnable() {
public void run() {
checkForStopped();
}
};
}
public void checkForStopped(){
int newPosition = getScrollY();
// the rest is irrelevant , but I hit a breakpoint here.
}
public void startScrollTask() {
mInitialPosition = getScrollY();
postDelayed( mScrollTask, mTimeUntilNextCheckForStopped );
}
测试:
@RunWith(RobolectricGradleTestRunner.class)
@Config(constants = BuildConfig.class, sdk = 21)
public class BbScrollViewTests {
@Test
public void test_startScrollTask(){
BbScrollView uut = spy( new BbScrollView( RuntimeEnvironment.application ) );
// This calls the method above that enqueues the runnable
uut.startScrollTask();
// Robolectric runs the runnable
ShadowLooper.runUiThreadTasksIncludingDelayedTasks();
// I can hit a breakpoint inside this method but verify() fails
verify( uut ).checkForStopped();
}
}
测试失败:
Wanted but not invoked:
bbScrollView.checkForStopped();
-> at com.myapp.android.BbKit.test.view.BbScrollViewTests.test_startScrollTask(BbScrollViewTests.java:62)
However, there were other interactions with this mock:
bbScrollView.startScrollTask();
-> at com.myapp.android.BbKit.test.view.BbScrollViewTests.test_startScrollTask(BbScrollViewTests.java:58)
bbScrollView.getScrollY();
-> at com.myapp.android.BbKit.test.view.BbScrollViewTests.test_startScrollTask(BbScrollViewTests.java:58)
bbScrollView.$$robo$getData();
-> at com.myapp.android.BbKit.test.view.BbScrollViewTests.test_startScrollTask(BbScrollViewTests.java:58)
bbScrollView.postDelayed(
com.myapp.android.BbKit.view.BbScrollView$1@7f830761,
100
);
-> at com.myapp.android.BbKit.test.view.BbScrollViewTests.test_startScrollTask(BbScrollViewTests.java:58)
bbScrollView.$$robo$getData();
-> at com.myapp.android.BbKit.test.view.BbScrollViewTests.test_startScrollTask(BbScrollViewTests.java:58)
我再说一遍:我在 verify() 检查的方法中打断点,但测试失败。我还尝试在 checkForStopped() 内部创建一个虚拟方法并对其进行验证,但无济于事。我还在 robolectric UI 线程调用的任一侧尝试了 1000ms thread.sleep。我的猜测是 robolectric 和 mockito 的反射材料之间的相互作用正在发生什么?
【问题讨论】:
标签: android mockito robolectric