简体   繁体   English

Robolectric单元测试引发异常

[英]Robolectric Unit Test throws Exception

I'm beginner at the whole testing stuff. 我是整个测试工作的初学者。 I want to build an activity with Robolectic but if I run the test it always throw a NullPointerException 我想用Robolectic建立一个活动,但是如果我运行测试,它总是会抛出NullPointerException

My Test SetUp 我的测试设置

@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();
}

My StackTrace 我的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)

I've tried this solution Robolectric Unit Test failing with Android Studio 2.3 updates but it didn't work for me. 我已经尝试过该解决方案Android Studio 2.3更新失败了,但它对我不起作用。

I ran into this issue yesterday when upgrading deps on an older project of mine. 我昨天在升级我的一个旧项目的部门时遇到了这个问题。 Turns out a "tighter integration with the toolchain" was added in robolectric 3.3+. 原来在robolectric 3.3+中添加了“与工具链更紧密的集成”。 For me the fix was to do the first step of robolectric's "Getting Started" page ( http://robolectric.org/getting-started/ ) and add the following to my app's build.gradle file. 对我来说,解决方法是执行robolectric的“入门”页面( http://robolectric.org/getting-started/ )的build.gradle ,并将以下内容添加到我的应用程序的build.gradle文件中。

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

Dependencies I'm using: 我正在使用的依赖项:

  • gradle wrapper: v4.1 Gradle包装器:v4.1
  • android gradle plugin: v3.0.1 android gradle插件:v3.0.1
  • robolectric: v3.8 robolectric:v3.8
  • android compile sdk: 26 Android编译SDK:26

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM