带有RxJava的Android Mocking文件返回NPE

时间:2019-07-11 08:40:20

标签: android unit-testing rx-java rx-java2 android-testing

我正在尝试模拟文件并使用Presenter将其测试保存到API,但是我正在获得有关RxJava调度程序的NPE。这是我的单元测试。

@RunWith(PowerMockRunner.class) @PrepareForTest({ ResponseBody.class})
public class FloorPlanImagePresenterTest {

  @Rule TrampolineSchedulerRule trampolineSchedulerRule = new TrampolineSchedulerRule();

  @Mock CustomersRepository customersRepository;
  @Mock FloorPlanImagePresenter.View view;
  @Mock ResponseBody responseBody;

  private FloorPlanImagePresenter presenter;

  @Before
  public void setUp() {
    presenter = new FloorPlanImagePresenter(customersRepository);
    presenter.setView(view);
  }

  @Test
  public void shouldDisplayOnFloorPlanImageEdited() {
    // Given
    File file = Mockito.mock(File.class);
    Response<ResponseBody> response = Response.success(200, responseBody);

    // Then
    Mockito.when(customersRepository.editBlueprint(1, 1, 1, file)).thenReturn(Single.just(response));
    presenter.editFlorPlanImage(1, 1, 1, "image_path");

    // When
    Mockito.verify(view).onFloorPlanImageEdited();
  }
}

这是我的演示者代码:

public class FloorPlanImagePresenter implements Presenter {

  private CustomersRepository customersRepository;
  private CompositeDisposable disposables;
  private View view;

  @Inject public FloorPlanImagePresenter(CustomersRepository customersRepository) {
    this.customersRepository = customersRepository;
  }

  public void editFlorPlanImage(int blueprintId, int customerId, int serviceLocationId, String imagePath) {
    disposables = RxUtil.initDisposables(disposables);

    Disposable disposable = customersRepository.editBlueprint(
        blueprintId,
        customerId,
        serviceLocationId,
        new File(imagePath)
    )
        .subscribeOn(Schedulers.io())
        .observeOn(AndroidSchedulers.mainThread())
        .subscribe(response -> {
          if (response.isSuccessful()) {
            view.onFloorPlanImageEdited();
          } else {
            view.onFloorPlanImageNotEdited();
          }
        }, throwable -> view.handleError(throwable));

    disposables.add(disposable);
  }

  public void setView(View view) {
    this.view = view;
  }

  @Override public void dispose() {
    RxUtil.dispose(disposables);
  }

  public interface View extends Presenter.View {

    void onFloorPlanImageEdited();

    void onFloorPlanImageNotEdited();
  }
}

这是我的崩溃:

java.lang.NullPointerException
	at com.anstar.presentation.agreements.graph.FloorPlanImagePresenter.editFlorPlanImage(FloorPlanImagePresenter.java:52)
	at com.anstar.presentation.agreements.FloorPlanImagePresenterTest.shouldDisplayOnFloorPlanImageEdited(FloorPlanImagePresenterTest.java:60)
	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.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.runTestMethod(PowerMockJUnit44RunnerDelegateImpl.java:310)
	at org.junit.internal.runners.MethodRoadie$2.run(MethodRoadie.java:89)
	at org.junit.internal.runners.MethodRoadie.runBeforesThenTestThenAfters(MethodRoadie.java:97)
	at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.executeTest(PowerMockJUnit44RunnerDelegateImpl.java:294)
	at org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl$PowerMockJUnit47MethodRunner.executeTestInSuper(PowerMockJUnit47RunnerDelegateImpl.java:127)
	at org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl$PowerMockJUnit47MethodRunner.access$100(PowerMockJUnit47RunnerDelegateImpl.java:59)
	at org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl$PowerMockJUnit47MethodRunner$LastRuleTestExecutorStatement.evaluate(PowerMockJUnit47RunnerDelegateImpl.java:148)
	at com.anstar.presentation.core.TrampolineSchedulerRule$1.evaluate(TrampolineSchedulerRule.java:21)
	at org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl$PowerMockJUnit47MethodRunner.executeTest(PowerMockJUnit47RunnerDelegateImpl.java:91)
	at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.runBeforesThenTestThenAfters(PowerMockJUnit44RunnerDelegateImpl.java:282)
	at org.junit.internal.runners.MethodRoadie.runTest(MethodRoadie.java:87)
	at org.junit.internal.runners.MethodRoadie.run(MethodRoadie.java:50)
	at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.invokeTestMethod(PowerMockJUnit44RunnerDelegateImpl.java:207)
	at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.runMethods(PowerMockJUnit44RunnerDelegateImpl.java:146)
	at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$1.run(PowerMockJUnit44RunnerDelegateImpl.java:120)
	at org.junit.internal.runners.ClassRoadie.runUnprotected(ClassRoadie.java:34)
	at org.junit.internal.runners.ClassRoadie.runProtected(ClassRoadie.java:44)
	at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.run(PowerMockJUnit44RunnerDelegateImpl.java:122)
	at org.powermock.modules.junit4.common.internal.impl.JUnit4TestSuiteChunkerImpl.run(JUnit4TestSuiteChunkerImpl.java:106)
	at org.powermock.modules.junit4.common.internal.impl.AbstractCommonPowerMockRunner.run(AbstractCommonPowerMockRunner.java:53)
	at org.powermock.modules.junit4.PowerMockRunner.run(PowerMockRunner.java:59)
	at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
	at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
	at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
	at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
	at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)

0 个答案:

没有答案