### Question:
ProjectStorage implements DataSource<Project> { @Override public List<Project> load() { if (!io.fileExists()) { return new ArrayList<Project>(); } ProjectsFileFormat projectList = null; Reader reader = io.getReader(); if (reader == null) { throw new UserVisibleException("Could not load project data from local storage."); } try { projectList = gson.fromJson(reader, ProjectsFileFormat.class); } catch (JsonSyntaxException ex) { throw new UserVisibleException("Local project storage corrupted"); } finally { try { reader.close(); } catch (IOException e) { return getProjectList(projectList); } } return getProjectList(projectList); } ProjectStorage(FileIO io); @Override List<Project> load(); @Override void save(List<Project> projects);  }### Answer:
@Test public void loadReturnsEmptyListIfFileDoesNotExist() { when(io.fileExists()).thenReturn(false); assertEquals(0, storage.load().size()); }
@Test(expected = UserVisibleException.class) public void loadThrowsIfReaderIsNull() { when(io.fileExists()).thenReturn(true); when(io.getReader()).thenReturn(null); storage.load(); }
@Test public void loadDoesNotThrowIfReaderCloseThrows() throws IOException { when(io.fileExists()).thenReturn(true); Reader reader = mock(Reader.class); when(reader.read(any(char[].class), anyInt(), anyInt())).thenReturn(-1); doThrow(new IOException("Foo")).when(reader).close(); when(io.getReader()).thenReturn(reader); storage.load(); verify(reader, times(1)).close(); }
@Test public void loadReturnsArrayWithCorrectSizeWithCorrectJSON() { Reader reader = createMockWithJson(); when(io.fileExists()).thenReturn(true); when(io.getReader()).thenReturn(reader); List<Project> list = storage.load(); assertEquals(1, list.size()); }
@Test public void loadReturnsArrayWithCorrectSizeIfReadIsSuccessfulButCloseThrowsException() throws IOException { Reader reader = createMockWithJson(); when(io.fileExists()).thenReturn(true); when(io.getReader()).thenReturn(reader); doThrow(new IOException("Foo")).when(reader).close(); List<Project> list = storage.load(); assertEquals(1, list.size()); }
@Test public void loadReturnsEmptyListIfReadingEmptyFile() throws IOException { Reader reader = mock(Reader.class); when(io.fileExists()).thenReturn(true); when(io.getReader()).thenReturn(reader); when(reader.read(any(char[].class), anyInt(), anyInt())).thenReturn(-1); assertEquals(0, storage.load().size()); }
@Test public void loadReturnsEmptyListIfLoadThrowsAndReadingEmptyFile() throws IOException { Reader reader = mock(Reader.class); when(io.fileExists()).thenReturn(true); when(io.getReader()).thenReturn(reader); when(reader.read(any(char[].class), anyInt(), anyInt())).thenReturn(-1); doThrow(new IOException("Foo")).when(reader).close(); assertEquals(0, storage.load().size()); }
@Test(expected = UserVisibleException.class) public void loadThrowsUserVisibleExceptionIfReadFails() throws IOException { Reader reader = mock(Reader.class); when(io.fileExists()).thenReturn(true); when(io.getReader()).thenReturn(reader); when(reader.read(any(char[].class), anyInt(), anyInt())).thenThrow(new IOException("Foo")); storage.load(); }