Source:
public void shouldRefundSuccessfullyForSandbox() { String externalChargeId = "chargeId"; Long amount = 100L; Long accountId = 2L; String providerName = "sandbox"; GatewayAccountEntity account = new GatewayAccountEntity(providerName, newHashMap(), TEST); account.setId(accountId); String reference = "reference"; ChargeEntity charge = aValidChargeEntity() .withGatewayAccountEntity(account) .withTransactionId(reference) .withStatus(CAPTURED) .build(); RefundEntity spiedRefundEntity = spy(aValidRefundEntity().withCharge(charge).build()); when(mockChargeDao.findByExternalIdAndGatewayAccount(externalChargeId, accountId)) .thenReturn(Optional.of(charge)); when(mockChargeDao.merge(charge)).thenReturn(charge); when(mockProviders.byName(SANDBOX)).thenReturn(mockProvider); setupWorldpayMock(reference, null); when(mockRefundDao.merge(any(RefundEntity.class))).thenReturn(spiedRefundEntity); ChargeRefundService.Response gatewayResponse = chargeRefundService.doRefund(accountId, externalChargeId, new RefundRequest(amount, charge.getAmount(), userExternalId)).get(); assertThat(gatewayResponse.getRefundGatewayResponse().isSuccessful(), is(true)); assertThat(gatewayResponse.getRefundGatewayResponse().getGatewayError().isPresent(), is(false)); assertThat(gatewayResponse.getRefundEntity(), is(spiedRefundEntity)); verify(mockChargeDao).findByExternalIdAndGatewayAccount(externalChargeId, accountId); verify(mockChargeDao).merge(charge); verify(mockRefundDao).persist(argThat(aRefundEntity(amount, charge))); verify(mockProvider).refund(argThat(aRefundRequestWith(charge, amount))); verify(mockRefundDao, times(2)).merge(any(RefundEntity.class)); verify(spiedRefundEntity).setStatus(RefundStatus.REFUNDED); verifyNoMoreInteractions(mockChargeDao, mockRefundDao); }

Target:
public void shouldRefundSuccessfullyForSandbox() { String externalChargeId = "chargeId"; Long amount = 100L; Long accountId = 2L; String providerName = "sandbox"; GatewayAccountEntity account = new GatewayAccountEntity(providerName, newHashMap(), TEST); account.setId(accountId); String reference = "reference"; ChargeEntity charge = aValidChargeEntity() .withGatewayAccountEntity(account) .withTransactionId(reference) .withStatus(CAPTURED) .build(); RefundEntity spiedRefundEntity = spy(aValidRefundEntity().withCharge(charge).build()); when(mockChargeDao.findByExternalIdAndGatewayAccount(externalChargeId, accountId)) .thenReturn(Optional.of(charge)); when(mockProviders.byName(SANDBOX)).thenReturn(mockProvider); setupWorldpayMock(reference, null); when(mockRefundDao.findById(any(Long.class))).thenReturn(Optional.of(spiedRefundEntity)); ChargeRefundService.Response gatewayResponse = chargeRefundService.doRefund(accountId, externalChargeId, new RefundRequest(amount, charge.getAmount(), userExternalId)).get(); assertThat(gatewayResponse.getRefundGatewayResponse().isSuccessful(), is(true)); assertThat(gatewayResponse.getRefundGatewayResponse().getGatewayError().isPresent(), is(false)); assertThat(gatewayResponse.getRefundEntity(), is(spiedRefundEntity)); verify(mockChargeDao).findByExternalIdAndGatewayAccount(externalChargeId, accountId); verify(mockRefundDao).persist(argThat(aRefundEntity(amount, charge))); verify(mockProvider).refund(argThat(aRefundRequestWith(charge, amount))); verify(mockRefundDao, times(2)).findById(any(Long.class)); verify(spiedRefundEntity).setStatus(RefundStatus.REFUNDED); verifyNoMoreInteractions(mockChargeDao, mockRefundDao); }