Advertisement
DSTRN

ServiceTest

Dec 14th, 2021 (edited)
729
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.97 KB | None | 0 0
  1. @RunWith(SpringRunner.class)
  2. @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
  3. public class A_ExamServiceTest {
  4.  
  5.     @Autowired
  6.     private ExamService examService;
  7.  
  8.     @MockBean
  9.     private StudentRepository mStudentRepository;
  10.  
  11.     @MockBean
  12.     private ExamRepository mExamRepository;
  13.  
  14.     private static final long EXAM_ID = 1l;
  15.  
  16.     private static final String STUDENT_ID = "sw25/2013";
  17.     private static final String STUDENT_FIRST_NAME = "Nikola";
  18.     private static final String STUDENT_LAST_NAME = "Nikolic";
  19.  
  20.     private static final Date PAST_DATE = new Date(System.currentTimeMillis() - 150000000);
  21.     private static final Date FUTURE_DATE = new Date(System.currentTimeMillis() + 150000000);
  22.  
  23.     @Test(expected = StudentNotFoundException.class)
  24.     public void shouldThrowItemNotFoundExceptionWhenStudentDoesNotExist() throws Exception {
  25.         Mockito.when(mStudentRepository.findByIdentificationNumber(STUDENT_ID)).thenReturn(Optional.empty());
  26.  
  27.         examService.examApplication(STUDENT_ID, EXAM_ID);
  28.     }
  29.  
  30.     @Test(expected = ExamNotFoundException.class)
  31.     public void shouldThrowItemNotFoundExceptionWhenExamDoesNotExist() throws Exception {
  32.         Student student = new Student(1l, STUDENT_ID, STUDENT_FIRST_NAME, STUDENT_LAST_NAME);
  33.  
  34.         Mockito.when(mStudentRepository.findByIdentificationNumber(STUDENT_ID)).thenReturn(Optional.of(student));
  35.         Mockito.when(mExamRepository.findById(EXAM_ID)).thenReturn(Optional.empty());
  36.  
  37.         examService.examApplication(STUDENT_ID, EXAM_ID);
  38.     }
  39.  
  40.     @Test
  41.     public void shouldReturnFalseWhenExamDateIsPassed() throws Exception {
  42.         Student student = new Student(1l, STUDENT_ID, STUDENT_FIRST_NAME, STUDENT_LAST_NAME);
  43.         Exam exam = new Exam(EXAM_ID, PAST_DATE, null, null);
  44.  
  45.         Mockito.when(mStudentRepository.findByIdentificationNumber(STUDENT_ID)).thenReturn(Optional.of(student));
  46.         Mockito.when(mExamRepository.findById(EXAM_ID)).thenReturn(Optional.of(exam));
  47.  
  48.         assertFalse(examService.examApplication(STUDENT_ID, EXAM_ID));
  49.     }
  50.  
  51.     @Test
  52.     public void shouldReturnFalseWhenExamIsPassed() throws Exception {
  53.         Student student = new Student(1l, STUDENT_ID, STUDENT_FIRST_NAME, STUDENT_LAST_NAME);
  54.         Exam exam = new Exam(EXAM_ID, true, FUTURE_DATE, null, null);
  55.  
  56.         Mockito.when(mStudentRepository.findByIdentificationNumber(STUDENT_ID)).thenReturn(Optional.of(student));
  57.         Mockito.when(mExamRepository.findById(EXAM_ID)).thenReturn(Optional.of(exam));
  58.  
  59.         assertFalse(examService.examApplication(STUDENT_ID, EXAM_ID));
  60.     }
  61.  
  62.     @Test
  63.     public void shouldReturnTrue() throws Exception {
  64.         Student student = new Student(1l, STUDENT_ID, STUDENT_FIRST_NAME, STUDENT_LAST_NAME);
  65.         Exam exam = new Exam(EXAM_ID, false, FUTURE_DATE, null, null);
  66.  
  67.         Mockito.when(mStudentRepository.findByIdentificationNumber(STUDENT_ID)).thenReturn(Optional.of(student));
  68.         Mockito.when(mExamRepository.findById(EXAM_ID)).thenReturn(Optional.of(exam));
  69.  
  70.         assertTrue(examService.examApplication(STUDENT_ID, EXAM_ID));
  71.         assertNotNull(exam.getStudent());
  72.         assertNotNull(exam.getApplicationDate());
  73.     }
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement