### Question:
CourseStorage implements DataSource<Course> { @Override public List<Course> load() { if (!io.fileExists()) { return new ArrayList<Course>(); } CoursesFileFormat courseList = null; Reader reader = io.getReader(); if (reader == null) { throw new UserVisibleException("Could not load course data from local storage."); } try { courseList = gson.fromJson(reader, CoursesFileFormat.class); } catch (JsonSyntaxException ex) { throw new UserVisibleException("Local course storage corrupted"); } finally { try { reader.close(); } catch (IOException e) { return getCourses(courseList); } } return getCourses(courseList); } CourseStorage(FileIO io); @Override List<Course> load(); @Override void save(List<Course> courses);  }### Answer:
@Test(expected = UserVisibleException.class) public void testExceptionIsThrownIfNullIO() throws UserVisibleException { this.io = null; storage.load(); }
@Test public void testExceptionIsThrownIfFileDoesntExist() throws UserVisibleException { when(io.fileExists()).thenReturn(false); assertTrue(storage.load() instanceof List && storage.load().size() == 0); }
@Test(expected = UserVisibleException.class) public void testExceptionIsThrownIfReaderIsNull() throws UserVisibleException { when(io.getReader()).thenReturn(null); storage.load(); }
@Test public void loadReturnsCorrectListOfCourses() { CourseList cl = buildMockCourseList(); String clJson = new Gson().toJson(cl); Reader reader = new StringReader(clJson); when(io.getReader()).thenReturn(reader); List<Course> returned = storage.load(); assertEquals("c1", returned.get(0).getName()); }