如何在Presenters方法中模拟新创建的对象?

时间:2019-12-02 08:07:28

标签: java android junit junit4 android-testing

我有使用方法payWorkOrder()的PaymentPresenter。该方法接受一些参数,并根据逻辑创建了两个新对象:

  1. WoPayment
  2. PaymentRequest

这是代码:

@RunWith(PowerMockRunner.class) @PrepareForTest({TextUtils.class, WoPayment.class, PaymentRequest.class})
public class PaymentPresenterTest extends BaseTest {

@Rule TrampolineSchedulerRule trampolineSchedulerRule = new  TrampolineSchedulerRule();

@Mock CustomersRepository customersRepository;
@Mock AgreementsRepository agreementsRepository;
@Mock WorkOrdersRepository workOrdersRepository;
@Mock PaymentPresenter.View view;
@Mock ResponseBody responseBody;

private PaymentPresenter presenter;

@Before public void setUp() {
mockTextUtils();
presenter = new PaymentPresenter(customersRepository,  agreementsRepository, workOrdersRepository);
presenter.setView(view);
}

public void payWorkOrderInvoice(int workOrderId, double amount, String paymentMethod, String checkNumber) {
disposables = RxUtil.initDisposables(disposables);

WoPayment woPayment = new WoPayment();

if(amount > 0) {
  woPayment.setAmount(amount);
} else {
  view.displayAmountShouldBeGreaterThanZero();
  return;
}

if(TextUtils.isEmpty(paymentMethod)) {
  view.displayPaymentMethodInvalid();
  return;
} else {
  woPayment.setPaymentMethod(paymentMethod);
}

if(paymentMethod.equalsIgnoreCase("Check") && TextUtils.isEmpty(checkNumber)) {
  view.displayReferenceNumberError();
  return;
} else {
  woPayment.setCheckNumber(checkNumber);
}

view.disablePayButton();

Disposable disposable = workOrdersRepository.payWorkOrderInvoice(workOrderId, new PaymentRequest(woPayment))
    .subscribeOn(Schedulers.io())
    .observeOn(AndroidSchedulers.mainThread())
    .subscribe(response -> {
      if(response.isSuccessful()) {
        view.displayWorkOrderInvoicePaid(response.body());
      } else {
        view.enablePayButton();
        view.displayWorkOrderInvoiceNotPaid();
      }
      }, throwable -> {
      view.enablePayButton();
      view.handleError(throwable);
    });

disposables.add(disposable);
}
}
}

这是我的单元测试:

@Test public void shouldPayWorkOrderInvoice() {
// Given
int workOrderId = 1;
double amount = 1.0;
String paymentMethod = "cash";
String checkNumber = "1";
WorkOrderDetails workOrderDetails = Mockito.mock(WorkOrderDetails.class);
Response<WorkOrderDetails> response = Response.success(200, workOrderDetails);
WoPayment woPayment = new WoPayment();
woPayment.setAmount(amount);
woPayment.setCheckNumber(checkNumber);
woPayment.setPaymentMethod(paymentMethod);
PaymentRequest paymentRequest = new PaymentRequest(woPayment);

// When
Mockito.when(workOrdersRepository.payWorkOrderInvoice(workOrderId, paymentRequest)).thenReturn(Single.just(response));
presenter.payWorkOrderInvoice(workOrderId, amount, paymentMethod, checkNumber);

// Then
Mockito.verify(view).displayWorkOrderInvoicePaid(workOrderDetails);
}

它在此处引发异常:

Disposable disposable = workOrdersRepository.payWorkOrderInvoice(workOrderId, new PaymentRequest(woPayment))
.subscribeOn(Schedulers.io())

但是它失败并显示以下堆栈跟踪:

  

java.lang.NullPointerException在   com.test.presentation.agreements.payment.PaymentPresenter.payWorkOrderInvoice(PaymentPresenter.java:168)     在   com.test.presentation.agreements.PaymentPresenterTest.shouldPayWorkOrderInvoice(PaymentPresenterTest.java:175)     在sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法)处   sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)     在   sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)     在java.lang.reflect.Method.invoke(Method.java:498)在   org.junit.internal.runners.TestMethod.invoke(TestMethod.java:68)在   org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl $ PowerMockJUnit44MethodRunner.runTestMethod(PowerMockJUnit44RunnerDelegateImpl.java:310)     在   org.junit.internal.runners.MethodRoadie $ 2.run(MethodRoadie.java:89)     在   org.junit.internal.runners.MethodRoadie.runBeforesThenTestThenAfters(MethodRoadie.java:97)     在   org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl $ PowerMockJUnit44MethodRunner.executeTest(PowerMockJUnit44RunnerDelegateImpl.java:294)     在   org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl $ PowerMockJUnit47MethodRunner.executeTestInSuper(PowerMockJUnit47RunnerDelegateImpl.java:127)     在   org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl $ PowerMockJUnit47MethodRunner.access $ 100(PowerMockJUnit47RunnerDelegateImpl.java:59)     在   org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl $ PowerMockJUnit47MethodRunner $ LastRuleTestExecutorStatement.evaluate(PowerMockJUnit47RunnerDelegateImpl.java:148)     在   com.test.presentation.core.TrampolineSchedulerRule $ 1.evaluate(TrampolineSchedulerRule.java:21)

1 个答案:

答案 0 :(得分:1)

在嘲笑中出现问题

您正在使用

  

Mockito.when(workOrdersRepository.payWorkOrderInvoice(workOrderId,   PaymentRequest))。thenReturn(Single.just(response));

NPE的原因是付款请求,而模拟与以下代码中传递的不一样

Disposable disposable = workOrdersRepository.payWorkOrderInvoice(workOrderId, new PaymentRequest(woPayment))
.subscribeOn(Schedulers.io())

作为回报,当原始代码正在订阅并且抛出空指针异常时,它什么也没提供

尝试 any(PaymentRequest.class),以及workOrderId anyInteger()