4

我将从项目设置开始(据我认为与当前问题相关):

安卓工作室 0.8.6

摇篮 1.12

robolectric-gradle-plugin 0.11.0

robolectric 2.3(也用 2.4-SNAPSHOT 测试)

com.android.support:support-v4 20.0.0

com.android.support:appcompat-v7 20.0.0

com.google.android.gms:play-services 5.2.08

我有一个扩展ActionBarActivity的活动,我想测试它。首要问题:

如果我使用以下设置,我会在尝试读取 AndroidManifest 时从 Robolectric 获得 NullPointerException:

@Config(emulateSdk = 18, manifest = "src/main/AndroidManifest.xml")
@RunWith(RobolectricTestRunner.class)
public class FilterActivityTest {}

尝试读取以下值时会出现此错误:

<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />

第一个错误似乎与此处发布的其他错误有关,例如https://github.com/robolectric/robolectric/issues/1025https://github.com/robolectric/robolectric-gradle-plugin/issues/56。通过使用那里的建议,我设法克服了这个错误。我的@Config 看起来像这样:

@Config(manifest = "build/intermediates/manifests/debug/AndroidManifest.xml", resourceDir = "../../../../build/intermediates/res/debug", emulateSdk = 18)

下一个错误是:

android.content.res.Resources$NotFoundException: Resource ID #0x7f0c0000
    at android.content.res.Resources.getValue(Resources.java:1118)
    at android.content.res.Resources.loadXmlResourceParser(Resources.java:2304)
    at android.content.res.Resources.getLayout(Resources.java:934)
    at android.support.v7.internal.view.SupportMenuInflater.inflate(SupportMenuInflater.java:115)
    at ro.cursurideschimb.user.FilterActivity.onCreateOptionsMenu(FilterActivity.java:56)
    at android.app.Activity.onCreatePanelMenu(Activity.java:2504)
    at android.support.v4.app.FragmentActivity.onCreatePanelMenu(FragmentActivity.java:225)
    at android.support.v7.app.ActionBarActivity.superOnCreatePanelMenu(ActionBarActivity.java:233)
    at android.support.v7.app.ActionBarActivityDelegateICS.onCreatePanelMenu(ActionBarActivityDelegateICS.java:146)
    at android.support.v7.app.ActionBarActivity.onCreatePanelMenu(ActionBarActivity.java:200)
    at android.support.v7.app.ActionBarActivityDelegateICS$WindowCallbackWrapper.onCreatePanelMenu(ActionBarActivityDelegateICS.java:293)
    at com.android.internal.policy.impl.PhoneWindow.preparePanel(PhoneWindow.java:413)
    at com.android.internal.policy.impl.PhoneWindow.doInvalidatePanelMenu(PhoneWindow.java:775)
    at com.android.internal.policy.impl.PhoneWindow$1.run(PhoneWindow.java:198)
    at org.robolectric.util.Scheduler.postDelayed(Scheduler.java:37)
    at org.robolectric.shadows.ShadowLooper.post(ShadowLooper.java:207)
    at org.robolectric.shadows.ShadowHandler.postDelayed(ShadowHandler.java:56)
    at android.os.Handler.postDelayed(Handler.java)
    at android.view.ViewRootImpl$RunQueue.executeActions(ViewRootImpl.java:6230)
    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1239)
    at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1004)
    at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5481)
    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
    at android.view.Choreographer.doCallbacks(Choreographer.java:562)
    at android.view.Choreographer.doFrame(Choreographer.java:532)
    at android.view.Choreographer$FrameHandler.handleMessage(Choreographer.java:664)
    at org.robolectric.shadows.ShadowHandler.routeMessage(ShadowHandler.java:125)
    at org.robolectric.shadows.ShadowHandler.access$100(ShadowHandler.java:25)
    at org.robolectric.shadows.ShadowHandler$1.run(ShadowHandler.java:110)
    at org.robolectric.util.Scheduler$PostedRunnable.run(Scheduler.java:162)
    at org.robolectric.util.Scheduler.runOneTask(Scheduler.java:107)
    at org.robolectric.util.Scheduler.advanceTo(Scheduler.java:92)
    at org.robolectric.util.Scheduler.advanceToLastPostedRunnable(Scheduler.java:68)
    at org.robolectric.util.Scheduler.unPause(Scheduler.java:25)
    at org.robolectric.shadows.ShadowLooper.unPause(ShadowLooper.java:228)
    at org.robolectric.shadows.ShadowLooper.runPaused(ShadowLooper.java:267)
    at org.robolectric.util.ActivityController.create(ActivityController.java:110)
    at org.robolectric.util.ActivityController.create(ActivityController.java:120)
    at ro.cursurideschimb.user.FilterActivityTest.setUp(FilterActivityTest.java:30)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:24)
    at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
    at org.robolectric.RobolectricTestRunner$2.evaluate(RobolectricTestRunner.java:267)
    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:194)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:160)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:134)

通过在 @Config 注释中添加 qualifiers = "v10" 解决了这个问题。

现在错误是:

java.lang.NullPointerException
at android.support.v7.app.ActionBarImplICS.setDisplayHomeAsUpEnabled(ActionBarImplICS.java:179)
at android.support.v7.app.ActionBarImplJB.setDisplayHomeAsUpEnabled(ActionBarImplJB.java:20)
at ro.cursurideschimb.user.FilterActivity.initialize(FilterActivity.java:72)
at ro.cursurideschimb.user.FilterActivity.onCreate(FilterActivity.java:105)
at android.app.Activity.performCreate(Activity.java:5133)
at org.fest.reflect.method.Invoker.invoke(Invoker.java:112)
at org.robolectric.util.ActivityController$1.run(ActivityController.java:113)
at org.robolectric.shadows.ShadowLooper.runPaused(ShadowLooper.java:265)
at org.robolectric.util.ActivityController.create(ActivityController.java:110)
at org.robolectric.util.ActivityController.create(ActivityController.java:120)
at ro.cursurideschimb.user.FilterActivityTest.setUp(FilterActivityTest.java:30)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:24)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
at org.robolectric.RobolectricTestRunner$2.evaluate(RobolectricTestRunner.java:267)
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:194)
at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
at org.junit.runner.JUnitCore.run(JUnitCore.java:160)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:134)

(Line 72 in FilterActivity reads like this:  actionBar.setDisplayHomeAsUpEnabled(true);)

现在,如果我将 reportSdk = 10 添加到 @Config 注释,我会回到缺少资源 ID 的错误。

这里的任何指针将不胜感激。感谢您的时间。

4

0 回答 0