### Question:
ProjectStorage implements DataSource<Project> { @Override public void save(List<Project> projects) { ProjectsFileFormat projectList = new ProjectsFileFormat(); projectList.setProjects(projects); if (io == null) { throw new UserVisibleException("Could not save project data to local storage."); } Writer writer = io.getWriter(); if (writer == null) { throw new UserVisibleException("Could not save project data to local storage."); } gson.toJson(projectList, writer); try { writer.close(); } catch (IOException e) { return; } } ProjectStorage(FileIO io); @Override List<Project> load(); @Override void save(List<Project> projects);  }### Answer:
@Test(expected = UserVisibleException.class) public void saveThrowsIfIOIsNull() { storage = new ProjectStorage(null); storage.save(projects); }
@Test(expected = UserVisibleException.class) public void saveThrowsIfWriterIsNull() { when(io.getWriter()).thenReturn(null); storage.save(projects); }
@Test public void saveDoesNotThrowIfCloseThrows() throws IOException { Writer writer = mock(Writer.class); when(io.getWriter()).thenReturn(writer); doThrow(new IOException("foo")).when(writer).close(); storage.save(projects); verify(writer, times(1)).close(); }