### Question:
IPKeyGenerator implements KeyGenerator { static void initWorkerId() { InetAddress address; try { address = InetAddress.getLocalHost(); } catch (final UnknownHostException e) { throw new IllegalStateException("Cannot get LocalHost InetAddress, please check your network!"); } byte[] ipAddressByteArray = address.getAddress(); DefaultKeyGenerator.setWorkerId((long) (((ipAddressByteArray[ipAddressByteArray.length - 2] & 0B11) << Byte.SIZE) + (ipAddressByteArray[ipAddressByteArray.length - 1] & 0xFF))); }  @Override Number generateKey();  }### Answer:
@Test public void testIP() throws UnknownHostException, NoSuchFieldException, IllegalAccessException { PowerMockito.mockStatic(InetAddress.class); PowerMockito.when(InetAddress.getLocalHost()).thenReturn(address); IPKeyGenerator.initWorkerId(); Field workerIdField = DefaultKeyGenerator.class.getDeclaredField("workerId"); workerIdField.setAccessible(true); assertThat(workerIdField.getLong(DefaultKeyGenerator.class), is(364L)); }
@Test public void testUnknownHost() throws UnknownHostException { PowerMockito.mockStatic(InetAddress.class); PowerMockito.when(InetAddress.getLocalHost()).thenThrow(new UnknownHostException()); exception.expect(IllegalStateException.class); exception.expectMessage("Cannot get LocalHost InetAddress, please check your network!"); IPKeyGenerator.initWorkerId(); }