0

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

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

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.

halfer
  • 19,824
  • 17
  • 99
  • 186
dudi
  • 5,523
  • 4
  • 28
  • 57

1 Answers1

0

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+. 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.

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

Dependencies I'm using:

  • gradle wrapper: v4.1
  • android gradle plugin: v3.0.1
  • robolectric: v3.8
  • android compile sdk: 26
Geoff
  • 2,892
  • 1
  • 22
  • 14
  • thank's for your hint but it didn't worked for me. I've tryed the same dependencies – dudi Mar 23 '18 at 07:51