Home > OS >  Mockito to test change in variables inside void methods
Mockito to test change in variables inside void methods

Time:10-03

I am trying to test change in arguments supplied to void method using ArgumentCaptor. But I am not able to achieve that . Can somebody please help me find out what exactly I am doing wrong. Using debug I know that list size is increased.

@InjectMocks private AuthValidationHelper authValidationHelper;

@Test
public void isAuthExpired(PaymentMethod paymentMethod, List<Error> errorList) {
    PaymentDetails paymentDetails = paymentMethod.getPaymentDetails();
    Date authExpirationTime = paymentDetails.getAuthorizationExpirationDate();
    boolean isAuthExpired = new Date().after(authExpirationTime);
    log.info("START : isAuthExpired for requestId: {}", paymentDetails.getRequestId());
    if (isAuthExpired) {
        log.info("START : validateOrderTotalAmount for result: {}", false);
        errorList.add(new Error(String.valueOf(PaymentErrorCodes.AUTH_EXPIRED),
                PaymentErrorCodes.AUTH_EXPIRED.getDescription()));
    }
}

Here params are null.

@Test
public void test_isAuthExpired_ExpiredAuth() throws ParseException {
List<Error> list = new ArrayList<>();
String request = inputProvider.createValidCreateOrderRequest_1();
PaymentTriggerBaseModel baseModel =
    ParserUtil.getObjectFromString(request, PaymentTriggerBaseModel.class);

authValidationHelper.isAuthExpired(baseModel.getPayment().getPaymentMethods().get(0), list);

ArgumentCaptor<Error> captor = ArgumentCaptor.forClass(Error.class);
final List<Error> params = captor.getAllValues(); --// here params are null
//assertTrue(params.get(0).getField().equalsIgnoreCase(0));
}

CodePudding user response:

You should first verify this argument.. like this

        List<Error> listMock = mock(List.class);
        authValidationHelper.isAuthExpired(mock(PaymentMethod.class), listMock);

        ArgumentCaptor<Error> captor = ArgumentCaptor.forClass(Error.class);
        verify(listMock).add(captor.capture());
        final List<Error> params = captor.getAllValues();

  • Related