### Question:
CourseStorage implements DataSource<Course> { @Override public void save(List<Course> courses) { CoursesFileFormat courseList = new CoursesFileFormat(); courseList.setCourses(courses); if (io == null) { throw new UserVisibleException("Could not save course data to local storage."); } Writer writer = io.getWriter(); if (writer == null) { throw new UserVisibleException("Could not save course data to local storage."); } gson.toJson(courseList, writer); try { writer.close(); } catch (IOException e) { return; } } CourseStorage(FileIO io); @Override List<Course> load(); @Override void save(List<Course> courses);  }### Answer:
@Test(expected = UserVisibleException.class) public void testExceptionIsThrownIfWriterIsNull() throws UserVisibleException { when(io.getWriter()).thenReturn(null); storage.save(new ArrayList<Course>()); }
@Test(expected = UserVisibleException.class) public void saveThrowsErrorWhenIoIsNull() { CourseStorage l = new CourseStorage(null); l.save(new ArrayList<Course>()); }
@Test(expected = UserVisibleException.class) public void saveThrowsIfWritesIsNull() { when(io.getWriter()).thenReturn(null); storage.save(new ArrayList<Course>()); }
@Test public void exceptionIsCaughtIfClosingWriterThrows() throws IOException { Writer writer = mock(Writer.class); when(io.getWriter()).thenReturn(writer); doThrow(new IOException("Foo")).when(writer).close(); storage.save(new ArrayList<Course>()); verify(writer, times(1)).close(); }