This is the method I am trying to test -
@Service
public class ShareableLinkService {
private PaymentService paymentService;
@Autowired
public ShareableLinkService(PaymentService paymentService) {
this.paymentService = paymentService;
}
public ResponseEntity<ResponseDTO> cancelSmartPay(Long id, String merchantRefId) {
..
responseDTO = paymentService.processCancelPayment(id, merchantRefId);
..
}
}
And following is the called method -
@Service
public class PaymentService {
@Transactional
public ResponseDTO processCancelPayment(Long param, String merchantRefId) {
TransactionRequest transactionRequest = transactionRequestService.findByMerchantIdAndMerchantRefId(param, merchantRefId);
..
}
Following is my test code -
@RunWith(MockitoJUnitRunner.class)
public class SmartPayMockitoServiceTest {
@Mock
private PaymentService paymentServiceNew;
when(paymentServiceNew.processCancelPayment(id,merchantRefId)).thenReturn(new ResponseDTO(Constants.API_RESPONSE_SUCCESS, "transaction cancelled"));
when(paymentServiceNew.processCancelPayment(id,merchantRefId)).thenReturn(new ResponseDTO(Constants.API_RESPONSE_SUCCESS, "transaction cancelled"));
assertEquals("1", shareableLinkService.cancelSmartPay(id, merchantRefId).getBody().getStatus().toString());
assertEquals("1", shareableLinkService.cancelSmartPay(payoutMerchantId, merchantRefId).getBody().getStatus().toString());
When I run the cancelSmartPay() call in the last statement, it actually calls the processCancelPayment() method.
I have already checked Mockito when().thenReturn calls the method unnecessarily
Update
I made it as per @Jalil's answer -
@Mock
private PaymentService paymentService;
@InjectMocks
private ShareableLinkService shareableLinkService;
However, still, the actual methods are being called. Also, when I am in debug mode, I get a java.lang.reflect.InvocationTargetException
.