Search code examples
javajunitmockito

Junit Code coverage for SocketTimeout exception inside ResourceAccessException


My goal is to get code coverage for my Adapter class. I have written Junit test case for read SocketTimeoutException(read timed out), it is not calling getSampleStatus() method. How to call this getSampleStatus() from my Test class.

rest class:
===========

    class MyAdapter
    {

        @Autowired
        BDSRequestForSubmitGIApp bdsRequestForSubmitGIApp;

        @Autowired
        RestTemplate restTemplate;


          public BDSSubmitResponse submitGIApplicationToBDS(String channelId,String customerId,String cinSuffix, String countryCode) throws CrossSellOffersException {
            BDSSubmitResponse bdsSubmitResponse = null;
            try {

                bdsSubmitResponse = restTemplate.postForObject(env.getProperty("bds_submit_gi_url"),
                        bdsRequestForSubmitGIApp, BDSSubmitResponse.class);

            } catch (ResourceAccessException e) {
                if (e.getCause().getClass().equals(SocketTimeoutException.class)) {
                    retrun getSampleStatus();// Junit is not covering this line
                } else {
                    throw new CrossSellOffersException("DOMAIN_CONNECT_FAILURE",
                            "Couldn’t contact domain sub-system(s). Please try after some time.");
                }
            }
            return bdsSubmitResponse;
          }

          private BDSSubmitResponse getSampleStatus(){
          //business logic
          }
    }


Junit :
=======

    @RunWith(MockitoJUnitRunner.class)
    class MyAdapterTest
    {
        @InjectMocks
        private MyAdapter crossSellOffersBDSAdapter;

        @Mock
        private RestTemplate restTemplate;

        @Mock
        BDSRequestForSubmitGIApp bdsRequestForSubmitGIApp;

        @Test
        public void submitGIApplicationToBDSReadTimeoutTest() throws Exception {
            final String ERROR_MESSAGE =
                    "I/O error on POST request for \"https://message-publisher.uat.apps.cs.sgp.dbs.com/message\": Read timed out; nested exception is java.net.SocketTimeoutException: Read timed out";
            Mockito.when(env.getProperty("bds_submit_gi_url")).thenReturn("https://message-publisher.uat.apps.cs.sgp.dbs.com/message");
            Mockito.when(restTemplate.postForObject(env.getProperty("bds_submit_gi_url"),
                bdsRequestForSubmitGIApp, BDSSubmitResponse.class)).thenThrow(new ResourceAccessException(ERROR_MESSAGE));

            BDSSubmitResponse response = crossSellOffersBDSAdapter.submitGIApplicationToBDS(requestBody, "MBSG",  "S9718016D", "00");
        }
    }

How to get code coverage for getSampleStatus() method?


Solution

  • ResourceAccessException class present in org.springframework.web.client package and its constructors are ResourceAccessException(String msg) ResourceAccessException(String msg, IOException ex)

    the below test case will work for ResourceAccessException

    @Test(expected = CrossSellOffersFulFilmentException.class)
        public void submitGIApplicationToBDSExceptionTest() throws Exception {
            //Given
            final String ERROR_MESSAGE = "ResourceAccessException";
            IOException exception = new IOException("IO exception");
    
            //When
            Mockito.when(env.getProperty("bds_submit_gi_url")).thenReturn("https://message-publisher.uat.apps.cs.sgp.dbs.com/message");
            Mockito.when(restTemplate.postForObject(env.getProperty("bds_submit_gi_url"),
                    bdsRequestForSubmitGIApp, BDSSubmitResponse.class)).thenThrow(new ResourceAccessException(ERROR_MESSAGE, exception));
    
            //Then
            crossSellOffersBDSAdapter.submitGIApplicationToBDS(requestBody, "MBSG", "S9718016D", "00");
        }