我有使用方法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在 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)
答案 0 :(得分:0)
stacktrace指出,在第168行的文件PaymentPresenter.java中,可以访问“空”对象,即未实例化。因此,请看该行并尝试找出为什么未实例化该对象的原因。没有文件的行号很难分辨。