@Test public void testCreateStudent_passnull() { // Given StudentDirectoryServiceImpl studentDirectoryService = new StudentDirectoryServiceImpl(); try { // When studentDirectoryService.createStudent(null); fail("An exception should have been thrown for a null value passed as a parameter."); } catch (Exception e) { // Then assertTrue(e instanceof NullPointerException); assertEquals("Unable to create a new student. The student cannot be null.", e.getMessage()); } }
@Test public void testCreateStudent_happypath() { // Given StudentDirectoryServiceImpl studentDirectoryService = new StudentDirectoryServiceImpl(); StudentDAO studentDAO = mock(StudentDAO.class); studentDirectoryService.setStudentDAO(studentDAO); Student testStudent = new Student(); testStudent.setFirstName("Michael"); testStudent.setLastName("Bluth"); // When studentDirectoryService.createStudent(testStudent); // Then ArgumentCaptor<Student> studentCaptor = ArgumentCaptor.forClass(Student.class); verify(studentDAO, times(1)).createStudent(studentCaptor.capture()); assertEquals("Michael", studentCaptor.getValue().getFirstName()); assertEquals("Bluth", studentCaptor.getValue().getLastName()); }