4

当我的 WebView 子类尝试在其 WebSettings 上调用 setDatabasePath 时,我试图让 Robolectric 2.1 与我的项目一起工作并获得 MustOverrideException。我有一个自定义应用程序类,它尝试在 onCreate 中创建 WebView 子类的实例。作为该 WebView 构造函数的一部分,它尝试设置数据库路径。

我原以为由于是从 WebView 调用 getSettings,所以调用会真正命中 ShadowWebView,它应该返回一个不会引发这些异常的 TestWebSettings 对象。相反,它似乎只是调用普通的 WebSettings.getSettings 并返回没有实现这些方法的东西。我尝试使用 @Config(shadows = ShadowWebView.class) 进行测试,但没有任何变化。我试图为我的专业 web 视图创建一个自定义 Shadow 并将其应用于测试,但它仍然调用常规自定义类。这是我尝试运行测试时看到的异常:

java.lang.RuntimeException: android.webkit.MustOverrideException: abstract function called: must be overriden!
    at org.robolectric.RobolectricTestRunner$2.evaluate(RobolectricTestRunner.java:231)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
    at org.robolectric.RobolectricTestRunner$1.evaluate(RobolectricTestRunner.java:177)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
    at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassExecuter.runTestClass(JUnitTestClassExecuter.java:80)
    at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassExecuter.execute(JUnitTestClassExecuter.java:47)
    at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassProcessor.processTestClass(JUnitTestClassProcessor.java:69)
    at org.gradle.api.internal.tasks.testing.SuiteTestClassProcessor.processTestClass(SuiteTestClassProcessor.java:49)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.gradle.messaging.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:35)
    at org.gradle.messaging.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:24)
    at org.gradle.messaging.dispatch.ContextClassLoaderDispatch.dispatch(ContextClassLoaderDispatch.java:32)
    at org.gradle.messaging.dispatch.ProxyDispatchAdapter$DispatchingInvocationHandler.invoke(ProxyDispatchAdapter.java:93)
    at com.sun.proxy.$Proxy2.processTestClass(Unknown Source)
    at org.gradle.api.internal.tasks.testing.worker.TestWorker.processTestClass(TestWorker.java:103)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.gradle.messaging.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:35)
    at org.gradle.messaging.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:24)
    at org.gradle.messaging.remote.internal.hub.MessageHub$Handler.run(MessageHub.java:355)
    at org.gradle.internal.concurrent.DefaultExecutorFactory$StoppableExecutorImpl$1.run(DefaultExecutorFactory.java:66)
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:895)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:918)
    at java.lang.Thread.run(Thread.java:680)
Caused by: android.webkit.MustOverrideException: abstract function called: must be overriden!
    at android.webkit.WebSettings.setDatabasePath(WebSettings.java:932)
    at com.example.android.jsbridge.ExWebView.init(ExWebView.java:237)
    at com.example.android.jsbridge.ExWebView.<init>(ExWebView.java:187)
    at com.example.android.jsbridge.ExWebView.getInstance(ExWebView.java:192)
    at com.example.android.ExApplication.onCreate(ExApplication.java:88)
    at org.robolectric.internal.ParallelUniverse.setUpApplicationState(ParallelUniverse.java:146)
    at org.robolectric.RobolectricTestRunner.setUpApplicationState(RobolectricTestRunner.java:387)
    at org.robolectric.RobolectricTestRunner$2.evaluate(RobolectricTestRunner.java:227)
4

2 回答 2

5

我喜欢 Jearil 的回答,但不必等待新版本更好。我扩展了 ShadowWebView 和 TestWebSettings 以添加未实现的方法。

扩展ShadowWebView.java

@Implements(value = WebView.class, inheritImplementationMethods = true)
public class ExtendedShadowWebView extends ShadowWebView{
private WebSettings webSettings = new ExtendedTestWebSettings();
  @Implementation
  public WebSettings getSettings() {
    return webSettings;
  }
}

ExtendedTestWebSettings.java

public class ExtendedTestWebSettings extends TestWebSettings {
      @Implementation
      public void setDefaultFontSize(int fontSize) {
           // This was the method I needed, replace with your setDatabasePath
      }
}

MyActivityTest.java

@RunWith(RobolectricTestRunner.class)
@Config(manifest = TestUtility.ANDROID_MANIFEST_XML,shadows=ExtendedShadowWebView.class)
public class MyActivityTest {
于 2014-01-28T01:03:11.623 回答
1

这最终成为 robolectric 本身的一个错误。他们使用为 Android 的 WebSettings 类提供实现的 TestWebSettings 类,但他们缺少 setDatabasePath 的实现。我提交了一个补丁https://github.com/robolectric/robolectric/commit/7eff08c896ccce4a01c887b89db64bcb808158e4已被接受并将进入未来的版本。我还用 setRenderPriority 修复了一个类似的错误。

于 2013-10-02T21:45:05.233 回答