Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @RunWith(SpringRunner.class)
- @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
- public class A_ExamServiceTest {
- @Autowired
- private ExamService examService;
- @MockBean
- private StudentRepository mStudentRepository;
- @MockBean
- private ExamRepository mExamRepository;
- private static final long EXAM_ID = 1l;
- private static final String STUDENT_ID = "sw25/2013";
- private static final String STUDENT_FIRST_NAME = "Nikola";
- private static final String STUDENT_LAST_NAME = "Nikolic";
- private static final Date PAST_DATE = new Date(System.currentTimeMillis() - 150000000);
- private static final Date FUTURE_DATE = new Date(System.currentTimeMillis() + 150000000);
- @Test(expected = StudentNotFoundException.class)
- public void shouldThrowItemNotFoundExceptionWhenStudentDoesNotExist() throws Exception {
- Mockito.when(mStudentRepository.findByIdentificationNumber(STUDENT_ID)).thenReturn(Optional.empty());
- examService.examApplication(STUDENT_ID, EXAM_ID);
- }
- @Test(expected = ExamNotFoundException.class)
- public void shouldThrowItemNotFoundExceptionWhenExamDoesNotExist() throws Exception {
- Student student = new Student(1l, STUDENT_ID, STUDENT_FIRST_NAME, STUDENT_LAST_NAME);
- Mockito.when(mStudentRepository.findByIdentificationNumber(STUDENT_ID)).thenReturn(Optional.of(student));
- Mockito.when(mExamRepository.findById(EXAM_ID)).thenReturn(Optional.empty());
- examService.examApplication(STUDENT_ID, EXAM_ID);
- }
- @Test
- public void shouldReturnFalseWhenExamDateIsPassed() throws Exception {
- Student student = new Student(1l, STUDENT_ID, STUDENT_FIRST_NAME, STUDENT_LAST_NAME);
- Exam exam = new Exam(EXAM_ID, PAST_DATE, null, null);
- Mockito.when(mStudentRepository.findByIdentificationNumber(STUDENT_ID)).thenReturn(Optional.of(student));
- Mockito.when(mExamRepository.findById(EXAM_ID)).thenReturn(Optional.of(exam));
- assertFalse(examService.examApplication(STUDENT_ID, EXAM_ID));
- }
- @Test
- public void shouldReturnFalseWhenExamIsPassed() throws Exception {
- Student student = new Student(1l, STUDENT_ID, STUDENT_FIRST_NAME, STUDENT_LAST_NAME);
- Exam exam = new Exam(EXAM_ID, true, FUTURE_DATE, null, null);
- Mockito.when(mStudentRepository.findByIdentificationNumber(STUDENT_ID)).thenReturn(Optional.of(student));
- Mockito.when(mExamRepository.findById(EXAM_ID)).thenReturn(Optional.of(exam));
- assertFalse(examService.examApplication(STUDENT_ID, EXAM_ID));
- }
- @Test
- public void shouldReturnTrue() throws Exception {
- Student student = new Student(1l, STUDENT_ID, STUDENT_FIRST_NAME, STUDENT_LAST_NAME);
- Exam exam = new Exam(EXAM_ID, false, FUTURE_DATE, null, null);
- Mockito.when(mStudentRepository.findByIdentificationNumber(STUDENT_ID)).thenReturn(Optional.of(student));
- Mockito.when(mExamRepository.findById(EXAM_ID)).thenReturn(Optional.of(exam));
- assertTrue(examService.examApplication(STUDENT_ID, EXAM_ID));
- assertNotNull(exam.getStudent());
- assertNotNull(exam.getApplicationDate());
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement