Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @RunWith(SpringRunner.class)
- @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
- public class Vezba_B_ExamControllerTest {
- @MockBean
- private ExamService mExamService;
- @Autowired
- private TestRestTemplate testRestTemplate;
- private static final Long EXAM_ID = 1l;
- @Test
- public void gradeExam_ThrowExamNotFoundException_404_NotFound() throws Exception {
- Mockito.when(mExamService.gradeExam(EXAM_ID, 10)).thenThrow(ExamNotFoundException.class);
- String url = "/exams";
- ExamDTO examDTO = new ExamDTO(EXAM_ID, 10);
- ResponseEntity<ExamNotFoundException> responseEntity = testRestTemplate.postForEntity(url, examDTO, ExamNotFoundException.class);
- assertEquals(HttpStatus.NOT_FOUND, responseEntity.getStatusCode());
- assertNotNull(responseEntity.getBody());
- }
- @Test
- public void gradeExam_ThrowNonExistingExamApplicationException_400_BadRequest() throws Exception {
- Mockito.when(mExamService.gradeExam(EXAM_ID, 10)).thenThrow(NonExistingExamApplicationException.class);
- String url = "/exams";
- ExamDTO examDTO = new ExamDTO(EXAM_ID, 10);
- ResponseEntity<NonExistingExamApplicationException> responseEntity = testRestTemplate.postForEntity(url, examDTO, NonExistingExamApplicationException.class);
- assertEquals(HttpStatus.BAD_REQUEST, responseEntity.getStatusCode());
- assertNotNull(responseEntity.getBody());
- }
- @Test
- public void gradeExam_ReturnFalse_400_BadRequest() throws Exception {
- Mockito.when(mExamService.gradeExam(EXAM_ID, 10)).thenReturn(false);
- String url = "/exams";
- ExamDTO examDTO = new ExamDTO(EXAM_ID, 10);
- ResponseEntity<Boolean> responseEntity = testRestTemplate.postForEntity(url, examDTO, Boolean.class);
- assertEquals(HttpStatus.BAD_REQUEST, responseEntity.getStatusCode());
- assertNotNull(responseEntity.getBody());
- }
- @Test
- public void gradeExam_ReturnTrue_200_OK() throws Exception {
- Mockito.when(mExamService.gradeExam(EXAM_ID, 10)).thenReturn(true);
- String url = "/exams";
- ExamDTO examDTO = new ExamDTO(EXAM_ID, 10);
- ResponseEntity<Boolean> responseEntity = testRestTemplate.postForEntity(url, examDTO, Boolean.class);
- assertEquals(HttpStatus.OK, responseEntity.getStatusCode());
- assertNotNull(responseEntity.getBody());
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement