Source:
public void shouldRefundSuccessfully_forWorldpay() { String externalChargeId = "chargeId"; Long amount = 100L; Long accountId = 2L; String providerName = "worldpay"; GatewayAccountEntity account = new GatewayAccountEntity(providerName, newHashMap(), TEST); account.setId(accountId); ChargeEntity charge = aValidChargeEntity() .withGatewayAccountEntity(account) .withTransactionId("transaction-id") .withStatus(CAPTURED) .build(); RefundEntity refundEntity = aValidRefundEntity().withCharge(charge).build(); RefundEntity spiedRefundEntity = spy(refundEntity); when(mockChargeDao.findByExternalIdAndGatewayAccount(externalChargeId, accountId)) .thenReturn(Optional.of(charge)); when(mockChargeDao.merge(charge)).thenReturn(charge); when(mockProviders.byName(WORLDPAY)).thenReturn(mockProvider); setupWorldpayMock(spiedRefundEntity.getExternalId(), null); when(mockRefundDao.merge(any(RefundEntity.class))).thenReturn(spiedRefundEntity); ChargeRefundService.Response gatewayResponse = chargeRefundService.doRefund(accountId, externalChargeId, new RefundRequest(amount, charge.getAmount(), null)).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).merge(any(RefundEntity.class)); verify(spiedRefundEntity).setStatus(RefundStatus.REFUND_SUBMITTED); verify(spiedRefundEntity).setReference(refundEntity.getExternalId()); verifyNoMoreInteractions(mockChargeDao, mockRefundDao); }

Target:
public void shouldRefundSuccessfully_forWorldpay() { String externalChargeId = "chargeId"; Long amount = 100L; Long accountId = 2L; String providerName = "worldpay"; GatewayAccountEntity account = new GatewayAccountEntity(providerName, newHashMap(), TEST); account.setId(accountId); ChargeEntity charge = aValidChargeEntity() .withGatewayAccountEntity(account) .withTransactionId("transaction-id") .withStatus(CAPTURED) .build(); RefundEntity refundEntity = aValidRefundEntity().withCharge(charge).build(); RefundEntity spiedRefundEntity = spy(refundEntity); when(mockChargeDao.findByExternalIdAndGatewayAccount(externalChargeId, accountId)) .thenReturn(Optional.of(charge)); when(mockChargeDao.merge(charge)).thenReturn(charge); when(mockProviders.byName(WORLDPAY)).thenReturn(mockProvider); setupWorldpayMock(spiedRefundEntity.getExternalId(), 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).merge(any(RefundEntity.class)); verify(spiedRefundEntity).setStatus(RefundStatus.REFUND_SUBMITTED); verify(spiedRefundEntity).setReference(refundEntity.getExternalId()); verifyNoMoreInteractions(mockChargeDao, mockRefundDao); }