Advertisement
DSTRN

ControllerTest_POST

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