我正在使用 ApplicationTestCase 测试一个 Android 应用.我想模拟我的一个 AsyncTasks(示例简化以显示问题):
I'm testing an Android app using an ApplicationTestCase. I want to mock one of my AsyncTasks (example simplified to show the problem):
public class Foo extends AsyncTask<Void,Void,Void> {
@Override
protected Void doInBackground(Void... unused) {
return null;
}
}
所以为了设置我的测试,我做了以下事情:
So to set up my tests, I did the following:
private Foo mockFoo;
@Override
protected void setUp() throws Exception {
super.setUp()
mockFoo = mock(Foo.class);
createApplication();
}
然后实际测试如下:
public void testAsyncTaskMock() {
mockFoo.execute();
verify(mockFoo).execute();
}
但是当 mockFoo.execute();
运行时出现异常:
But I get an exception when mockFoo.execute();
runs:
java.lang.NullPointerException: Attempt to invoke virtual method 'int android.os.AsyncTask$Status.ordinal()' on a null object reference
为什么模拟 AsyncTask 的技术不起作用?
请注意,删除 createApplication();
在这个简单的案例中会导致问题消失,但对于我的实际测试,我确实需要创建应用程序.
Note that removing createApplication();
causes the problem to go away in this simple case, but for my actual testing I do need the application to be created.
AsyncTask.execute 是最终的,Mockito 无法模拟最终类或方法.
具体来说,这是因为 Java 可以在编译时解析链接,这意味着 Mockito 无法使用其生成的子类和方法覆盖来改变行为.
Specifically, this is because Java can resolve the link at compile time, which means Mockito can't use its generated subclasses and method overrides to change the behavior.
您可以选择使用 Powermock,它使用特殊的类加载器来重写旧的行为,或者 Robolectric,它的作用相同,但用 Android 特定的测试友好的替代实现(shadows")替换类 包括一个用于 AsyncTask 的.
You may choose to use Powermock, which uses a special classloader to rewrite the old behavior, or Robolectric, which does the same but replaces classes with Android-specific test-friendly alternate implementations ("shadows") including one for AsyncTask.
这篇关于为什么模拟不能与 AsyncTask 一起工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!