IpAddressResolver { public String resolveToHostName(final String ip) { String host = ""; try { InetAddress address = inetAddressFactory.createByName(ip); host = address.getHostName(); } catch (UnknownHostException e) { logger.debug(DEBUG_MESSAGE + host, e); host = String.format(UNKNOWN_HOST_TEMPLATE, ip); } return host; }  String resolveToHostName(final String ip);  }
@Test public void testResolveToHostNameShouldReturnHostName() throws UnknownHostException { String expectedHostName = "expectedHostName"; given(inetAddressFactory.createByName("anIPAddress")).willReturn(inetAddress); given(inetAddress.getHostName()).willReturn(expectedHostName); String actualHostName = underTest.resolveToHostName("anIPAddress"); assertEquals(expectedHostName, actualHostName); }
@Test public void testResolveToHostNameShouldReturnUnknownHostNameWhenUnknownHostExceptionOccurs() throws UnknownHostException { String expectedHostName = "UNKNOWN HOST(anIPAddress)"; given(inetAddressFactory.createByName("anIPAddress")).willThrow(new UnknownHostException()); String actualHostName = underTest.resolveToHostName("anIPAddress"); assertEquals(expectedHostName, actualHostName); }