我试图让Robolectric2.1处理我的项目,并在WebView的子类试图调用其Websettings上的setDatabasePath时获得mustOverrideeException。我有一个自定义应用程序类,它试图在oncreate中创建webview子类的实例。作为webview构造函数的一部分,它试图设置数据库路径。
我本以为既然getsettings是从webview调用的,那么这个调用就会真正命中shadowwebview,它应该返回一个testwebsettings对象,而这个对象不会抛出那些异常。相反,它似乎只是调用普通的websettings.getsettings并返回一些没有实现这些方法的内容。我尝试使用@config(shadows=shadowwebview.class)进行测试,但没有任何更改。我已经尝试创建我的专用web视图的自定义阴影并将其应用于测试,但它仍然调用常规自定义类。下面是我在尝试运行测试时看到的异常:
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)
最佳答案
我喜欢杰里尔的回答,但不必等待新的版本更好。我扩展了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;
}
}
扩展的TestWebsettings.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 {