Advertisement
DSTRN

ControllerTest_GET

Dec 14th, 2021
763
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.37 KB | None | 0 0
  1. @RunWith(SpringRunner.class)
  2. @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
  3. public class Vezba_A_ExamControllerTest {
  4.  
  5.     @MockBean
  6.     private ExamService mExamService;
  7.    
  8.     private static final Long EXAM_ID = 1l;
  9.    
  10.     private static final String STUDENT_ID = "sw61-2018";
  11.    
  12.     @Autowired
  13.     private TestRestTemplate testRestTemplate;
  14.    
  15.     @Test
  16.     public void applyForExam_ExamNotFoundException_404_NotFound() throws Exception {
  17.         Mockito.when(mExamService.examApplication(STUDENT_ID, EXAM_ID)).thenThrow(ExamNotFoundException.class);
  18.        
  19.         String url = "/exams/" + EXAM_ID + "?identificationNumber=" + STUDENT_ID;
  20.        
  21.         ResponseEntity<ExamNotFoundException> responseEntity = testRestTemplate.getForEntity(url, ExamNotFoundException.class);
  22.        
  23.         assertEquals(HttpStatus.NOT_FOUND, responseEntity.getStatusCode());
  24.         assertNotNull(responseEntity.getBody());
  25.     }
  26.    
  27.     @Test
  28.     public void applyForExam_StudentNotFoundException_404_NotFound() throws Exception {
  29.         Mockito.when(mExamService.examApplication(STUDENT_ID, EXAM_ID)).thenThrow(StudentNotFoundException.class);
  30.        
  31.         String url = "/exams/" + EXAM_ID + "?identificationNumber=" + STUDENT_ID;
  32.        
  33.         ResponseEntity<StudentNotFoundException> responseEntity = testRestTemplate.getForEntity(url, StudentNotFoundException.class);
  34.        
  35.         assertEquals(HttpStatus.NOT_FOUND, responseEntity.getStatusCode());
  36.         assertNotNull(responseEntity.getBody());
  37.     }
  38.    
  39.     @Test
  40.     public void applyForExam_InvalidDate_400_BadRequest() throws Exception {
  41.         Mockito.when(mExamService.examApplication(STUDENT_ID, EXAM_ID)).thenReturn(false);
  42.         String url = "/exams/" + EXAM_ID + "?identificationNumber=" + STUDENT_ID;
  43.         ResponseEntity<Boolean> responseEntity = testRestTemplate.getForEntity(url, Boolean.class);
  44.         assertEquals(HttpStatus.BAD_REQUEST, responseEntity.getStatusCode());
  45.         assertNotNull(responseEntity.getBody());
  46.         assertFalse(responseEntity.getBody());
  47.     }
  48.    
  49.     @Test
  50.     public void applyForExam_200_OK() throws Exception {
  51.         Mockito.when(mExamService.examApplication(STUDENT_ID, EXAM_ID)).thenReturn(true);
  52.         String url = "/exams/" + EXAM_ID + "?identificationNumber=" + STUDENT_ID;
  53.         ResponseEntity<Boolean> responseEntity = testRestTemplate.getForEntity(url, Boolean.class);
  54.         assertEquals(HttpStatus.OK, responseEntity.getStatusCode());
  55.         assertNotNull(responseEntity.getBody());
  56.         assertTrue(responseEntity.getBody());
  57.     }
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement