Solution 1: Spring profile
Assuming api.example.com
is defined in your spring application.properties
, the most common behavior is to override this property using the spring boot fallback mechanism associated to spring profile.
For example assuming api.example.com
is associated to the property api.host
key, You could create a spring profile integration-testing
and its associated application properties files application-integration-testing.properties
under src/test/resources
. This file will contain api.host=localhost
whereas the production application.properties
contains api.host=api.example.com
.
You annotate your test with @ActiveProfiles("integration-testing")
and voila, api.host
value will be localhost
but only for testing.
Solution 2: Custom DNS resolver
It does not seems possible to have a custom resolver with the default implementation (HttpURLConnection). You can use a RestTemplate
with an implementation of ClientHttpRequestFactory which allows a custom DNS resolver eg. the Apache HttpClient HttpComponentsClientHttpRequestFactory, see this answer for more detail.
Solution 3: Mock Rest template
You can use a RestTemplate
mock using MockRestServiceServer. This solution does not involve a real http server.