lundi 11 juillet 2016

Unit testing Realm + Dagger 2 with Robolectric & Mockito

So I am working on this little project that uses Dagger 2 for dependency injection and Realm as a database.

I am unit testing it with Robolectric and Mockito (with Powermock). From previous research (and a lot of pain) I realised testing Realm is pretty laborious, but has been done in the past here.

Now, my project has a very similar setup and structure to the one linked above.

When I run my unit tests, all of them pass except for one that gives me a very cryptic message that looks as follows:

java.lang.NullPointerException
at org.robolectric.internal.ShadowExtractor.extract(ShadowExtractor.java:5)
at org.robolectric.Shadows.shadowOf(Shadows.java:1190)
at org.robolectric.shadows.CoreShadowsAdapter.getMainLooper(CoreShadowsAdapter.java:37)
at org.robolectric.util.ComponentController.<init>(ComponentController.java:31)
at org.robolectric.util.ComponentController.<init>(ComponentController.java:23)
at org.robolectric.util.ActivityController.<init>(ActivityController.java:40)
at org.robolectric.util.ActivityController.of(ActivityController.java:32)
at org.robolectric.Robolectric.buildActivity(Robolectric.java:82)
at org.robolectric.Robolectric.buildActivity(Robolectric.java:78)
at org.robolectric.Robolectric.setupActivity(Robolectric.java:86)
at uk.co.placona.tradesafe.view.EditActivityTest.ActivityShouldNotBeNull(EditActivityTest.java:54)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.internal.runners.TestMethod.invoke(TestMethod.java:68)
at org.powermock.modules.junit4.internal.impl

The line of code specified on the error above is:

activity = Robolectric.setupActivity(EditActivity.class);

The activity exists, and has a TradeRepository injected to it when it starts up.

The activity in question can be found here along with the rest of the code. I have been trying to debug this for about 3 days now with no success. Every other unit test I create works fine, except any unit test that is used by an Activity, which makes me think I'm probably missing something really obvious.

Would be happy to clarify any questions here. With many thanks!

Aucun commentaire:

Enregistrer un commentaire