我有带有方法payWorkOrder((的PaymentPresenter。该方法接受一些参数,并根据逻辑创建了两个新对象:
- 沃支付
- 付款请求
这是代码:
@RunWith(PowerMockRunner.class) @PrepareForTest({TextUtils.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);
}
但它失败并显示以下堆栈跟踪:
java.lang.NullPointerException at com.test.presentation.agreements.payment.PaymentPresenter.payWorkOrderInvoice(PaymentPresenter.java:168( 在 com.test.presentation.agreements.PaymentPresenterTest.shouldPayWorkOrderInvoice(PaymentPresenterTest.java:175( at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method( at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62( 在 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( 在 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(
堆栈跟踪表示,在文件PaymentPresenter中.java在第168行中,可以访问"null"的对象,即未实例化。因此,请查看该行并尝试找出对象未实例化的原因。如果没有文件的行号,很难分辨。