Robolectric单元测试抛出异常

时间:2018-02-13 08:21:18

标签: android testing nullpointerexception stack-trace robolectric

我是整个测试人员的初学者。我想用Robolectic构建一个活动但是如果我运行测试它总是抛出一个NullPointerException

我的测试设置

@RunWith(RobolectricTestRunner.class)
@Config(constants = BuildConfig.class)
public class HomeActivityTest  {

private HomeActivity activity;


@Before
public void setup() throws Exception{
  activity   = Robolectric.buildActivity(HomeActivity.class)
            .create()
            .resume()
            .get();
}

我的StackTrace

java.lang.NullPointerException
at org.robolectric.res.ThemeStyleSet$OverlayedStyle.equals(ThemeStyleSet.java:67)
at org.robolectric.res.ThemeStyleSet.apply(ThemeStyleSet.java:29)
at org.robolectric.shadows.ShadowAssetManager.applyThemeStyle(ShadowAssetManager.java:729)
at android.content.res.AssetManager.applyThemeStyle(AssetManager.java)
at android.content.res.ResourcesImpl$ThemeImpl.applyStyle(ResourcesImpl.java:1177)
at android.content.res.Resources$Theme.applyStyle(Resources.java:1404)
at android.view.ContextThemeWrapper.onApplyThemeResource(ContextThemeWrapper.java:186)
at android.app.Activity.onApplyThemeResource(Activity.java:4248)
at android.view.ContextThemeWrapper.initializeTheme(ContextThemeWrapper.java:198)
at android.view.ContextThemeWrapper.setTheme(ContextThemeWrapper.java:140)
at android.app.Activity.setTheme(Activity.java:4240)
at org.robolectric.shadows.ShadowActivity.callAttach(ShadowActivity.java:216)
at org.robolectric.android.controller.ActivityController.attach(ActivityController.java:41)
at org.robolectric.android.controller.ActivityController.of(ActivityController.java:25)
at org.robolectric.Robolectric.buildActivity(Robolectric.java:84)
at org.robolectric.Robolectric.buildActivity(Robolectric.java:80)
at com.my.application.activities.HomeActivityTest.setup(HomeActivityTest.java:32)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:24)
at org.robolectric.internal.SandboxTestRunner$2.evaluate(SandboxTestRunner.java:253)
at org.robolectric.internal.SandboxTestRunner.runChild(SandboxTestRunner.java:130)
at org.robolectric.internal.SandboxTestRunner.runChild(SandboxTestRunner.java:42)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.robolectric.internal.SandboxTestRunner$1.evaluate(SandboxTestRunner.java:84)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:51)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
at com.intellij.rt.execution.application.AppMainV2.main(AppMainV2.java:131)

我已经尝试过这个解决方案Robolectric Unit Test failing with Android Studio 2.3 updates,但它对我没用。

1 个答案:

答案 0 :(得分:0)

我昨天遇到了这个问题,在我的旧项目上升级deps。结果是与工具链的紧密集成"添加到robolectric 3.3+中。对我来说,解决方法是完成robolectric"入门"的第一步。页面(http://robolectric.org/getting-started/)并将以下内容添加到我应用的build.gradle文件中。

android {
    testOptions {
        unitTests {
            includeAndroidResources = true
        }
    }
}

依赖关系我使用:

  • gradle wrapper:v4.1
  • android gradle plugin:v3.0.1
  • robolectric:v3.8
  • android compile sdk:26