SAP / cloud-sdk-java

Use the SAP Cloud SDK for Java to reduce development effort when building applications on SAP Business Technology Platform that communicate with SAP solutions and services such as SAP S/4HANA Cloud, SAP SuccessFactors, and many others.
Apache License 2.0
22 stars 13 forks source link

How to Mock getDestination for Unit Testing Java Methods with HTTP Requests? #577

Closed Jagan1002 closed 3 weeks ago

Jagan1002 commented 3 weeks ago

I’m working on unit testing a Java method that interacts with external HTTP destinations and performs HTTP requests. I’m having difficulty covering the method with JUnit tests due to its dependencies on external services.

Here is the method I need to test:

import static com.sap.cloud.sdk.cloudplatform.connectivity.DestinationAccessor.getDestination;

public String getResponseAsString(String path) {
    HttpDestinationProperties httpDestination = getDestination(destinationName()).asHttp();
    HttpClient client = getHttpClient(httpDestination);
    try {
        String requestUrl = httpDestination.getUri() +            HttpClientUtils.replaceSpecialSymbolsForIpSystem(path);
        log.info("GET response as a string: {} ", requestUrl);

        return client.execute(new HttpGet(requestUrl), getResponseHandler());
    } catch (IOException e) {
        log.error(CONNECTION_ABORTED, e.getMessage());
    }

    return EMPTY;
}

My challenges:

Mocking getDestination(destinationName()).asHttp(): How can I mock the destination and its properties?

What I’ve tried:

I attempted to use Mockito for mocking, but I'm unsure how to properly mock the getDestination and HttpClient methods.

junits I tried:

@RunWith(PowerMockRunner.class)
@PrepareForTest({ DestinationAccessor.class})
public class HttpDestinationClientTest {
    @Mock
    private HttpClient httpClient;
    @Mock
    private HttpDestinationProperties httpDestination;
    @Mock
    private Destination destination;
    @Mock
    private DestinationAccessor destinationAccessor;
    @InjectMocks
    private HttpDestinationClient client = new HttpDestinationClient() {
        @Override
        protected String destinationName() {
            return "mockDestination";
        }
    };

    @Before
    public void setUp() throws Exception {
        PowerMockito.mockStatic(DestinationAccessor.class);
         HttpDestination mockHttpDestination = Mockito.mock(HttpDestination.class);
         Destination mockDestination = mock(Destination.class);
         when(mockDestination.asHttp()).thenReturn(mockHttpDestination);
         when(DestinationAccessor.getDestination("mockDestination")).thenReturn((mockDestination));
    }
    @Test
    public void testGetResponseAsString() throws IOException, InterruptedException {
        client.getResponseAsString("/path");
    }
}

This junits throws error:
Destination Access exception: failed to get destination

MatKuhr commented 3 weeks ago

Answered here. If you need further help please update your SO question 😉