下午好。
我不知道下一步该怎么做。已经尝试并更新了Robolectric,并更改了一些实现测试跑步者(这是自定义(。我在build.gradle中升级了库,测试中的应用程序开始出现错误:
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
我的测试类:
@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);
}
}
我的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'
更新PushWoosh库后我有一个错误。
提前提供帮助,谢谢!
P.S。这些文件实际上更大(原则上的测试是合乎逻辑的(,但是所有文件都以我上面显示的唯一错误而下降。
该问题是由试图创建一个地理系数的robolectric引起的,在初始化PushWoosh之前,该杂志均不能存在。
您可以通过删除此线程中描述的不必要的接收器来找到解决问题:
使用AndroidJunitrunner和AndroidJunit4