Replace localhost lookup with static IP to fix test.

Calling InetAddress.getLocalHost() will cause a lookup to occur that may
fail with a java.net.UnknownHostException if the system the test is
running on is not configured correctly.

This is often fixed by echoing "127.0.0.1 $HOSTNAME" to /etc/hosts, but
in this case it seems easier to pick a static IP string to avoid the
lookup entirely and prevent false negatives in the test.
This commit is contained in:
Adam Tanner 2015-07-20 14:01:45 -07:00
parent 8e570ee3a2
commit 457541611c
1 changed files with 5 additions and 5 deletions

View File

@ -35,11 +35,11 @@ public class DefaultInetAddressTypeAdapterTest extends TestCase {
}
public void testInetAddressSerializationAndDeserialization() throws Exception {
InetAddress localhost = InetAddress.getLocalHost();
String localInetAddress = gson.toJson(localhost);
assertEquals("\"" + localhost.getHostAddress() + "\"", localInetAddress);
InetAddress address = InetAddress.getByName("8.8.8.8");
String jsonAddress = gson.toJson(address);
assertEquals("\"8.8.8.8\"", jsonAddress);
InetAddress value = gson.fromJson(localInetAddress, InetAddress.class);
assertEquals(localhost, value);
InetAddress value = gson.fromJson(jsonAddress, InetAddress.class);
assertEquals(value, address);
}
}