NetworkInterfaceValidator implements Validator<NetworkInterface> { @Override public boolean isValid(NetworkInterface networkInterface) { boolean isValid = false; try { isValid = networkInterface != null && !networkInterface.isLoopback() && networkInterface.isUp(); } catch (SocketException e) { e.printStackTrace(); } return isValid; } @Inject  NetworkInterfaceValidator(); @Override boolean isValid(NetworkInterface networkInterface);  }
@Test public void shouldEvaluateAsInvalidGivenNullInterface() throws Exception { boolean isValid = networkInterfaceValidator.isValid(null); assertThat(isValid).isFalse(); }
@Test public void shouldEvaluateAsInvalidGivenLoopbackInterface() throws Exception { when(mockNetworkInterface.isLoopback()).thenReturn(true); boolean isValid = networkInterfaceValidator.isValid(mockNetworkInterface); assertThat(isValid).isFalse(); }
@Test public void shouldEvaluateAsInvalidGivenDownInterface() throws Exception { when(mockNetworkInterface.isLoopback()).thenReturn(false); when(mockNetworkInterface.isUp()).thenReturn(false); boolean isValid = networkInterfaceValidator.isValid(mockNetworkInterface); assertThat(isValid).isFalse(); }
@Test public void shouldEvaluateAsValidGivenValidInterface() throws Exception { when(mockNetworkInterface.isLoopback()).thenReturn(false); when(mockNetworkInterface.isUp()).thenReturn(true); boolean isValid = networkInterfaceValidator.isValid(mockNetworkInterface); }