This is not related to cloud sdk per se, but more regarding mocking the s4 endpoints which we usually use c sdk to query. We want to do this for our load test, where we would not want the load test to go till s4 endpoint. We are considering using wiremock, to mock the endpoints, but the question is, whether the mocking logic in wiremock itself will contribute not in an insignificant manner to the metrics we are taking. If it would, then the metric becomes somewhwat unreliable since we want the apps performance metric not the mock framework's.
Other approach would be to use a mock server application dedicated for this, so that from app we would not have to do any mocking. Just route the call to the mock server app(using a mock destination perhaps)
My question to you guys is, have you ever encountered this use case? Perhaps yourself, or maybe from a consumer of yours. I would like to know how other teams in SAP solved this problem.
Thanks, Sachin
In cases like yours, where the entire system (including responses from external services) should be tested, we usually recommend using Wiremock. This is, because Wiremock is rather easy to setup and works well-enough for regular testing scenarios. However, as you also pointed out, Wiremock introduces significant runtime overhead for the tested code and, thus, rending performance measurements of any kind more or less useless.
Hence, you could try mocking the HttpClient
using Mockito instead:
BasicHttpResponse page = new BasicHttpResponse(new BasicStatusLine(HttpVersion.HTTP_1_1, 200, "OK"));
page.setEntity(new StringEntity("hello world!));
HttpClient httpClient = mock(HttpClient.class);
doReturn(page).when(httpClient).execute(any(HttpUriRequest.class));
This enables fine-grained control over what your applications retrieves from the mocked endpoint without introducing any kind of actual network actions.
Using the code shown above obviously requires your application under test to actually use the mocked httpClient
.
Assuming you are using the SAP Cloud SDK in your application, this can be achieved by overriding the HttpClientCache
used in the HttpClientAccessor
with a custom implementation that returns your mocked client, like so:
class MockedHttpClientCache implements HttpClientCache
{
@Nonnull
@Override
public Try<HttpClient> tryGetHttpClient(@Nonnull final HttpDestinationProperties destination, @Nonnull final HttpClientFactory httpClientFactory) {
return Try.of(yourMockedClient);
}
@Nonnull
@Override
public Try<HttpClient> tryGetHttpClient(@Nonnull final HttpClientFactory httpClientFactory) {
return Try.of(yourMockedClient);
}
}
// in your test code:
HttpClientAccessor.setHttpClientCache(new MockedHttpClientCache());