4

Is it possible to override a single DNS lookup in the java jvm?

I am running a junit test in java. The test makes a connection to an external server host1. Instead, I want the jvm to contact localhost when it tries to contact host1.

The test succeeds if /etc/hosts file contains:

127.0.0.1 host1

I am wondering if it can be done without modifying the hosts file.

We are using java 8. The junit test uses mockito & powermock libraries. It will run on Mac OS Mojave & CentOS (some container in some kubernetes pod on gcp/aws).

Thanks for reading.

user674669
  • 10,681
  • 15
  • 72
  • 105
  • I have the same question, I'm developping a multi hostname API and needs it to respond differently upon the host used in incoming http request. If you're trying to mock/redirect calls to an external service, you could rewrite code to use configuration and mock the configuration or use different properties for the test environment. – Benoit Jul 10 '20 at 07:19

1 Answers1

0

You may want to look at poisoning your user environment instead of the system /etc/hosts by tricking the system to load another /etc/hosts. See this for a Linux solution: https://unix.stackexchange.com/questions/57459/how-can-i-override-the-etc-hosts-file-at-user-level

Thomas
  • 3,119
  • 2
  • 16
  • 22