0

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.

Sandeepan Nath
  • 9,966
  • 17
  • 86
  • 144

1 Answers1

0

can you please share how you instantiate the shareableLinkService object.

It should be something like this

@RunWith(MockitoJUnitRunner.class)
public class SmartPayMockitoServiceTest {

    @Mock
    private PaymentService paymentServiceNew;

    @InjectMocks
    private ShareableLinkService shareableLinkService;

    // the rest of the code

}

Your test should be inside a method annotated with @Test

@Test
void testMethod(){

    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());

}
Jalil.Jarjanazy
  • 839
  • 1
  • 9
  • 22