我正在编写一个需要侦听UDP广播消息的Android应用程序。
我正在尝试在监听器类中模拟AsyncTask,以便对其进行单元测试。
问题是我总是得到错误 java.lang.RuntimeException:android.os.AsyncTask中的方法执行没有被模拟。
TimeZone
答案 0 :(得分:1)
AsyncTask.execute is final, and Mockito can't mock final classes or methods.
Cannot mock final methods - their real behavior is executed without any exception. Mockito cannot warn you about mocking final methods so be vigilant.
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.
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.