@Test public void testImportStudents_exception() throws Exception { // Given StudentDirectoryServiceImpl studentDirectoryService = new StudentDirectoryServiceImpl(); Logger logger = mock(Logger.class); studentDirectoryService.setLogger(logger); StudentDAO studentDAO = mock(StudentDAO.class); studentDirectoryService.setStudentDAO(studentDAO); MultipartFile multipartFile = mock(MultipartFile.class); IOException exception = new IOException("This is a dummy IOException."); when(multipartFile.getInputStream()).thenThrow(exception); // When studentDirectoryService.importStudents(multipartFile); // Then ArgumentCaptor<String> errorCaptor = ArgumentCaptor.forClass(String.class); verify(logger, times(1)).error(errorCaptor.capture()); assertEquals(exception, errorCaptor.getValue()); }
@Test public void testImportStudents() throws Exception { // Given StudentDirectoryServiceImpl studentDirectoryService = new StudentDirectoryServiceImpl(); StudentDAO studentDAO = mock(StudentDAO.class); studentDirectoryService.setStudentDAO(studentDAO); MultipartFile multipartFile = mock(MultipartFile.class); StringBuffer buffer = new StringBuffer(); buffer.append("Bart, Simpson\n"); buffer.append("Lisa, Simpson"); InputStream stream = new ByteArrayInputStream(buffer.toString().getBytes(StandardCharsets.UTF_8)); when(multipartFile.getInputStream()).thenReturn(stream); // When studentDirectoryService.importStudents(multipartFile); // Then ArgumentCaptor<Student> studentCaptor = ArgumentCaptor.forClass(Student.class); verify(studentDAO, times(2)).createStudent(studentCaptor.capture()); assertEquals("Bart", studentCaptor.getAllValues().get(0).getFirstName()); assertEquals("Simpson", studentCaptor.getAllValues().get(0).getLastName()); assertEquals("Lisa", studentCaptor.getAllValues().get(1).getFirstName()); assertEquals("Simpson", studentCaptor.getAllValues().get(1).getLastName()); }