Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Robolectric: NullPointerException in setupActivity()

I'm trying to use Robolectric for testing my activities, but I'm always getting a NPE, when trying to setup the Activity with Robolectric. I have followed the guide from the robolectric.org website.

Here is my code:

@Config(constants = BuildConfig.class, sdk = Build.VERSION_CODES.LOLLIPOP, manifest = "src/main/AndroidManifest.xml")
@RunWith(RobolectricTestRunner.class)
@Ignore
public class MainActivityUnitTest {

    private MainActivity activity;

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

    @Test
    public void dummyTest() {
        assertTrue(true);
    }
} 

Some other information: I'm using Dagger2 for DI in the project. The exception I'm getting:

java.lang.NullPointerException
    at org.robolectric.internal.ShadowExtractor.extract(ShadowExtractor.java:5)
    at org.robolectric.Shadows.shadowOf(Shadows.java:1165)
    at org.robolectric.shadows.CoreShadowsAdapter.getMainLooper(CoreShadowsAdapter.java:42)
    at org.robolectric.util.ComponentController.<init>(ComponentController.java:31)
    at org.robolectric.util.ActivityController.<init>(ActivityController.java:35)
    at org.robolectric.util.ActivityController.of(ActivityController.java:27)
    at org.robolectric.Robolectric.setupActivity(Robolectric.java:46)
    at com.me.myapplication.MainActivityUnitTest.setup(MainActivityUnitTest.java:32)
like image 852
Tamás Kozmér Avatar asked Mar 02 '17 09:03

Tamás Kozmér


1 Answers

try: https://github.com/robolectric/robolectric/issues/3169

"AGP 3.0 includes changes made to better support unit tests (by including merged resources, assets + AndroidManifest.xml as first class citizens along with the Java code).

This can be enabled by adding the following configuration to your build.gradle files

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

"

like image 63
RVscript Avatar answered Oct 23 '22 15:10

RVscript