### Question:
GetLocalIP { protected static List<String> getIPFromInputStream(BufferedReader reader) { if (reader != null) { Set<String> allLines = new LinkedHashSet<>(); String line = null; try { while ((line = reader.readLine()) != null) { Matcher matcher = kIPPattern.matcher(line); if (matcher.matches()) { allLines.add(line); } } } catch (IOException e) { e.printStackTrace(); } return allLines.stream().collect(Collectors.toList()); } return Collections.emptyList(); } private  GetLocalIP(); static Optional<String> getIp(); static List<String> getAllIps(); static void main(String[] pArgs);  }### Answer:
@Test public void testReaderMultipleIpsDifferent() throws IOException{ BufferedReader br = mock(BufferedReader.class); when(br.readLine()).thenReturn("10.18.85.186","10.18.85.187",(String)null); List<String> returnVal = GetLocalIP.getIPFromInputStream(br); assertNotNull(returnVal); assertFalse(returnVal.isEmpty()); assertEquals("10.18.85.186", returnVal.get(0)); }
@Test public void testNullReader() { List<String> returnVal = GetLocalIP.getIPFromInputStream(null); assertNotNull(returnVal); assertTrue(returnVal.isEmpty()); }
@Test public void testReaderIOException() throws IOException{ BufferedReader br = mock(BufferedReader.class); when(br.readLine()).thenThrow(IOException.class); List<String> returnVal = GetLocalIP.getIPFromInputStream(br); assertNotNull(returnVal); assertTrue(returnVal.isEmpty()); }
@Test public void testReaderNoIPs() throws IOException{ BufferedReader br = mock(BufferedReader.class); when(br.readLine()).thenReturn("Test String",(String)null); List<String> returnVal = GetLocalIP.getIPFromInputStream(br); assertNotNull(returnVal); assertTrue(returnVal.isEmpty()); }
@Test public void testReaderMultipleIpsSame() throws IOException{ BufferedReader br = mock(BufferedReader.class); when(br.readLine()).thenReturn("10.18.85.186","10.18.85.186",(String)null); List<String> returnVal = GetLocalIP.getIPFromInputStream(br); assertNotNull(returnVal); assertFalse(returnVal.isEmpty()); assertEquals("10.18.85.186", returnVal.get(0)); }