androidrobolectricpushwoosh

ExceptionInInitializerError when updated Pushwoosh


Good afternoon.

I do not know what to do next. Already tried and updated robolectric and changed some implementations test runner (it is custom). I upgraded the library in build.gradle and the application on the tests began to fall with an error:

java.lang.ExceptionInInitializerError
    at com.pushwoosh.location.geofencer.GeofenceReceiver.<init>(Unknown Source)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
    at org.robolectric.util.ReflectionHelpers.callConstructor(ReflectionHelpers.java:321)
    at org.robolectric.internal.bytecode.ShadowImpl.newInstanceOf(ShadowImpl.java:20)
    at org.robolectric.shadow.api.Shadow.newInstanceOf(Shadow.java:35)
    at org.robolectric.shadows.ShadowApplication.registerBroadcastReceivers(ShadowApplication.java:138)
    at org.robolectric.shadows.ShadowApplication.bind(ShadowApplication.java:127)
    at org.robolectric.shadows.CoreShadowsAdapter.bind(CoreShadowsAdapter.java:71)
    at org.robolectric.android.internal.ParallelUniverse.setUpApplicationState(ParallelUniverse.java:107)
    at org.robolectric.RobolectricTestRunner.beforeTest(RobolectricTestRunner.java:290)
    at org.robolectric.internal.SandboxTestRunner$2.evaluate(SandboxTestRunner.java:203)
    at org.robolectric.internal.SandboxTestRunner.runChild(SandboxTestRunner.java:109)
    at org.robolectric.internal.SandboxTestRunner.runChild(SandboxTestRunner.java:36)
    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:63)
    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 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 com.intellij.rt.execution.application.AppMainV2.main(AppMainV2.java:131)
Caused by: java.lang.NullPointerException
    at com.pushwoosh.location.d.a.<init>(Unknown Source)
    at com.pushwoosh.location.internal.a.<clinit>(Unknown Source)
    ... 33 more

my Test class:

@RunWith(CustomRobolectricRunner.class)
@Config(constants = BuildConfig.class)
@FragmentScope
public class LentaPresenterTest {

    @Mock
    private LentaItem lentaItemMock;
    @Mock
    private ChampionatRepository championatDataRepositoryMock;
    @Mock
    private CommentsRepository commentsRepositoryMock;
    @Mock
    private LocalRepository localRepositoryMock;
    @Inject
    BookmarkRepository bookmarkRepository;
    @Mock
    private LentaView lentaViewMock;
    @Mock
    private Exception exceptionMock;
    @Inject
    Context context;
    @Inject
    LocalRepository localRepository;
    @Mock
    private NetworkInfoManager networkInfoManager;
    @Inject
    protected LentaPresenter lentaPresenterTest;

    private List<LentaItem> lentaItemListMock;
    private Observable<GetLentaInteractor.LentaResponce> observable;
    private Observable<Boolean> isShowFontObservable;

    private TestScheduler testScheduler = Schedulers.test();

    @Before
    public void setUp() {
        MockitoAnnotations.initMocks(this);
        TestApp.getTestComponent().createTestActivityComponent(new TestActivityModule(mock(Activity.class))).
                createTestFragmentComponent(new TestFragmentModule(mock(Fragment.class))).inject(this);
        lentaPresenterTest = spy(lentaPresenterTest);

        assert lentaPresenterTest.getLentaInteractor != null;

        lentaItemListMock = new ArrayList<>();
        lentaItemListMock.add(lentaItemMock);

        observable = Observable.just(new GetLentaInteractor.LentaResponce(lentaItemListMock, 2));
        lentaPresenterTest.setView(lentaViewMock);
        lentaPresenterTest.getLentaInteractor = spy(new GetLentaInteractor(championatDataRepositoryMock, commentsRepositoryMock, localRepositoryMock, bookmarkRepository, networkInfoManager) {

            @Override
            public Subscription execute(LentaFilter lentaFilter, Subscriber<LentaResponce> subscriber) {
                return observable.subscribe(subscriber);
            }
        });

        lentaPresenterTest.isShowRatingViewInteractor = spy(new IsShowRatingViewInteractor(localRepository, context) {
            @Override
            protected Observable<Boolean> buildObservable() {
                return isShowFontObservable;
            }
        });

        lentaPresenterTest.isShowRatingViewInteractor.setJobScheduler(testScheduler);
        lentaPresenterTest.isShowRatingViewInteractor.setIuScheduler(testScheduler);
    }


    @Test
    public void interactorOnErrorTest() {
        observable = Observable.error(exceptionMock);

        lentaPresenterTest.initialize();

        verify(lentaViewMock, times(1)).startProgress();
        verify(lentaViewMock, times(1)).setSelectedSportsKinds(anyList());
        verify(lentaViewMock, times(1)).showLayoutWithInformation();
        verify(lentaViewMock, times(1)).stopProgress();
        verifyNoMoreInteractions(lentaViewMock);
    }

    @Test
    public void interactorOnErrorTest_showTop() {
        observable = observable.concatWith(Observable.<GetLentaInteractor.LentaResponce>error(exceptionMock));

        lentaPresenterTest.initialize();
        verify(lentaViewMock, times(1)).startProgress();
        verify(lentaViewMock, times(1)).setSelectedSportsKinds(anyList());
        verify(lentaViewMock, times(1)).inflateData(anyList());
        verify(lentaViewMock, times(1)).hideBottomErrorMessage();
        verify(lentaViewMock, times(1)).showTopErrorMessage();
        verify(lentaViewMock, times(1)).stopProgress();
        verifyNoMoreInteractions(lentaViewMock);
    }

    @Test
    public void interactorOnNextTest_first_request_true() {
        isShowFontObservable = Observable.just(true);
        lentaPresenterTest.lentaFilter = LentaFilter.EMPTY;
        lentaPresenterTest.initialize();

        testScheduler.triggerActions();
        verify(lentaPresenterTest.getLentaInteractor, times(1)).execute(any(LentaFilter.class), any(Subscriber.class));
        verify(lentaPresenterTest.isShowRatingViewInteractor, times(1)).execute(any(Subscriber.class));
        verify(lentaViewMock, times(1)).inflateData(lentaItemListMock);
    }

    @Test
    public void interactorOnNextTest_first_request_false() {
        isShowFontObservable = Observable.just(true);
        lentaPresenterTest.lentaFilter = LentaFilter.EMPTY.lentaBefore("before id");
        lentaPresenterTest.initialize();

        testScheduler.triggerActions();
        verify(lentaPresenterTest.getLentaInteractor, times(1)).execute(any(LentaFilter.class), any(Subscriber.class));
        verify(lentaPresenterTest.isShowRatingViewInteractor, times(1)).execute(any(Subscriber.class));
        verify(lentaViewMock, times(1)).appendData(lentaItemListMock);
    }
}

My build.gradle:

testCompile 'org.robolectric:robolectric:3.3.2'
testCompile 'org.robolectric:shadows-multidex:3.3.2'
testCompile 'org.robolectric:shadows-play-services:3.1'

I got an error after updating the pushwoosh library.

For help in advance thanks!

P.S. The files are actually larger (it is logical that in principle the tests), but all fall with the only error that I showed above.


Solution

  • The issue is caused by Robolectric trying to create a GeofenceReceiver, which just cannot exist before Pushwoosh is initialized.

    You can find workaround the issue by removing unnecessary receivers like described in this thread:

    Android Instrumentation test java.lang.UnsatisfiedLinkError on using AndroidJunitRunner and AndroidJUnit4