Marc_Bob
Marc_Bob

Reputation: 119

Robolectric 3.0 throws Nullpointer on DefaultPackageManager.getActivityInfo()

I try to setup Robolectric 3.0 with our App. We have multiple dex files and the Activity I try to test extends a normal Activity. In the build.gradle I do this:

testCompile 'junit:junit:4.12'
testCompile "org.robolectric:robolectric:3.0"
testCompile('org.robolectric:shadows-multidex:3.0')

and the Test looks like this:

@Config(constants = BuildConfig.class, sdk = VERSION_CODES.KITKAT)
@RunWith(RobolectricGradleTestRunner.class)
public class FirstStartActivityTest
{

private FirstStartActivity activity;

@Before
public void setup() {
    activity = Robolectric.setupActivity(FirstStartActivity.class);
}

@Test
public void failingTest() {
    assertNotNull("This is supposed to fail!", null);
}

}

It keeps throwing this Exception:

java.lang.NullPointerException at org.robolectric.res.builder.DefaultPackageManager.getActivityInfo(DefaultPackageManager.java:173) at org.robolectric.util.ActivityController.getActivityInfo(ActivityController.java:65) at org.robolectric.util.ActivityController.attach(ActivityController.java:51) at org.robolectric.util.ActivityController$1.run(ActivityController.java:121) at org.robolectric.shadows.ShadowLooper.runPaused(ShadowLooper.java:304) at org.robolectric.shadows.CoreShadowsAdapter$2.runPaused(CoreShadowsAdapter.java:45) at org.robolectric.util.ActivityController.create(ActivityController.java:118) at org.robolectric.util.ActivityController.create(ActivityController.java:129) at org.robolectric.util.ActivityController.setup(ActivityController.java:210) at org.robolectric.Robolectric.setupActivity(Robolectric.java:46) at FirstStartActivityTest.setup(FirstStartActivityTest.java:27) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:606)...

A similar question was already posed here: Robolectric 3.0 Nullpointer on DefaultPackageManager.getActivityInfo() but the solution did not work for me.

FYI I use the SupportLibraryVersion support-v4-22.2.0

If anyone has any idea how to solve this let me know. Thank you.

Upvotes: 2

Views: 799

Answers (2)

Tanapruk Tangphianphan
Tanapruk Tangphianphan

Reputation: 803

For me, I imported the BuildConfig from the wrong module. Change the import to the correct one and the problem is gone.

Upvotes: 1

mtotschnig
mtotschnig

Reputation: 1551

I have seen this error when I was setting an applicationIdSuffix for a buildType. This seems to confuse Robolectric's resource resolution mechanism.

Upvotes: 2

Related Questions