Validation { @Nonnull public File[] getListOfChildren(@Nonnull final File path) { final File[] retVal; if (path.exists() && path.isDirectory() && path.listFiles() != null) { retVal = path.listFiles(); } else { retVal = new File[0]; } return retVal; }  boolean isValidUsbDeviceCandidate(@Nonnull final File file); @Nonnull File[] getListOfChildren(@Nonnull final File path);  }
@Test public void getListOfChildren_no_children() { final File[] children = new File[0]; final File mockFile = Mockito.mock(File.class); Mockito.when(mockFile.exists()).thenReturn(true); Mockito.when(mockFile.isDirectory()).thenReturn(true); Mockito.when(mockFile.listFiles()).thenReturn(children); final File[] result = cut.getListOfChildren(mockFile); Assert.assertSame(children, result); }
@Test public void getListOfChildren_valid_children() { final File[] children = new File[5]; final File mockFile = Mockito.mock(File.class); Mockito.when(mockFile.exists()).thenReturn(true); Mockito.when(mockFile.isDirectory()).thenReturn(true); Mockito.when(mockFile.listFiles()).thenReturn(children); final File[] result = cut.getListOfChildren(mockFile); Assert.assertSame(children, result); }
@Test(expected = NullPointerException.class) public void getListOfChildren_is_null() { final File mockFile = null; cut.getListOfChildren(mockFile); }
@Test public void getListOfChildren_not_existent() { final File mockFile = Mockito.mock(File.class); Mockito.when(mockFile.exists()).thenReturn(false); final File[] result = cut.getListOfChildren(mockFile); Assert.assertEquals(0, result.length); }
@Test public void getListOfChildren_not_directory() { final File mockFile = Mockito.mock(File.class); Mockito.when(mockFile.exists()).thenReturn(true); Mockito.when(mockFile.isDirectory()).thenReturn(false); final File[] result = cut.getListOfChildren(mockFile); Assert.assertEquals(0, result.length); }
@Test public void getListOfChildren_null_children() { final File mockFile = Mockito.mock(File.class); Mockito.when(mockFile.exists()).thenReturn(true); Mockito.when(mockFile.isDirectory()).thenReturn(true); Mockito.when(mockFile.listFiles()).thenReturn(null); final File[] result = cut.getListOfChildren(mockFile); Assert.assertEquals(0, result.length); }