I am trying to mock the new instance of CourseLoader
in the method I'm testing so when I call loadAll()
it returns my mock.
I have setup a mock()
in the unit test but it seems to be ignoring what I'm doing, is some of the setup wrong?
Method I'm testing
public ArrayList<Module> getAssignedModulesWithDetails() {
CourseLoader courseLoader = new CourseLoader();
ArrayList<Module> assignedModules = new ArrayList<>();
for (Course course : courseLoader.loadAll()) {
ArrayList<Module> courseModules = course.getModules();
for (Module module : courseModules) {
if (module.getInstructor() != null && module.getInstructor().equals(this.getId())) {
assignedModules.add(module);
}
}
}
return assignedModules;
}
Unit test
@Test
public void testYouCanGetAssignedModulesWithDetails() {
CourseLoader courseLoaderMock = mock(CourseLoader.class);
ArrayList<Course> mockedCourses = new ArrayList<>();
ArrayList<Module> mockedModules = new ArrayList<>();
Module module1 = new Module(this.module1, "Module1", true, 4, false, this.id);
Module module2 = new Module(this.module2, "Module2", true, 4, false, this.id);
Module module3 = new Module(this.module3, "Module3", true, 4, false, this.id);
mockedModules.add(module1);
mockedModules.add(module2);
mockedModules.add(module3);
Course mockedCourse = new Course(UUID.randomUUID(), "Mocked Course", true, mockedModules);
mockedCourses.add(mockedCourse);
when(courseLoaderMock.loadAll()).thenReturn(mockedCourses);
ArrayList<Module> assignedModulesWithDetails = this.instructor.getAssignedModulesWithDetails();
assertEquals(0, assignedModulesWithDetails.size());
}