Robolectric does not use ShadowWebView as a superclass of a class that extends WebView. Instead, a MustOverrideException

I am trying to get Robolectric 2.1 to work with my project and get a MustOverrideException when my subclass of WebView tries to call setDatabasePath on it WebSettings. I have my own application class that is trying to instantiate a subclass of WebView in onCreate. As part of this WebView constructor, it tries to set the database path.

I would think that since getSettings is being called from WebView, this call would really hit ShadowWebView, which should have returned a TestWebSettings object that does not throw these exceptions. Instead, it looks like it just calls the usual WebSettings.getSettings settings and returns something that does not have these methods. I tried using @Config (shadows = ShadowWebView.class) for my test, but without changes. I tried to create a custom shadow for my custom web view and apply it to the test, but it still calls the regular custom class. Here is the exception that I see when trying to run my tests:

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
source share
2 answers

Jearil, . ShadowWebView TestWebSettings, , .

ExtendedShadowWebView.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 {
+5

​​ robolectric. TestWebSettings, Android WebSettings, setDatabasePath. https://github.com/robolectric/robolectric/commit/7eff08c896ccce4a01c887b89db64bcb808158e4, . setRenderPriority.

+1

All Articles