Why doesn#39;t mocking work with AsyncTask?(为什么模拟不能与 AsyncTask 一起工作?)
问题描述
我正在使用 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 无法模拟最终类或方法.
- 不能模拟最终方法 - 它们的真实行为会毫无例外地执行.Mockito 无法警告您模拟 final 方法,因此请保持警惕.
具体来说,这是因为 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 一起工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:为什么模拟不能与 AsyncTask 一起工作?


- 如何使用WebFilter实现授权头检查 2022-01-01
- Eclipse 插件更新错误日志在哪里? 2022-01-01
- 将log4j 1.2配置转换为log4j 2配置 2022-01-01
- 从 finally 块返回时 Java 的奇怪行为 2022-01-01
- Jersey REST 客户端:发布多部分数据 2022-01-01
- C++ 和 Java 进程之间的共享内存 2022-01-01
- Spring Boot连接到使用仲裁器运行的MongoDB副本集 2022-01-01
- Safepoint+stats 日志,输出 JDK12 中没有 vmop 操作 2022-01-01
- Java包名称中单词分隔符的约定是什么? 2022-01-01
- value & 是什么意思?0xff 在 Java 中做什么? 2022-01-01