mercredi 18 septembre 2019

How can I make to throw this catch with JUnit

I got a function that make an update of an issue in jira and i want to throw the catch ussing JUnit.

This is the function than i got:

@PutMapping (value = "/update/{issueKey}")
    public ResponseEntity<ResponseDTO>
           updateIssue(@Validated @RequestBody EventDTO eventDTO, BindingResult result, @PathVariable String issueKey)
    {
        logger.info("Entra en /update con el payload: {}", eventDTO);

        if (result.hasErrors())
        {
            ErrorResponseDTO errorResponseDTO = ErrorResponseDTO.getErrorResponseDTOFromBinding(result, messageSource);
            return new ResponseEntity<>(errorResponseDTO, HttpStatus.BAD_REQUEST);
        }
        try
        {
            SuccessResponseDTO successResponseDTO = jiraService.update(eventDTO, issueKey);
            logger.info("/update response {} ", successResponseDTO);
            return new ResponseEntity<>(successResponseDTO, HttpStatus.OK);
        }
        catch (EywaException eywaException)
        {
            logger.error("Se ha producido un error en actualizar un issue", eywaException);
            ErrorResponseDTO responseDTO = new ErrorResponseDTO();


            String errorMessage = messageSource.getMessage(eywaException.getMessage(), null,
                    LocaleContextHolder.getLocale());
            responseDTO.addErrorResponseDTO(eywaException.getMessage().split("\\.")[0], errorMessage);
            return new ResponseEntity<>(responseDTO, HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

This is what i have in the JUnit

@Test
    public void updateIssue_NonExistanceIssue_shouldReturnFail() throws Exception
    {
        when(jiraService.update(eventDTO, "")).thenReturn(successResponseDTO);

        String json = "{\"summary\":\""+eventDTO.getSummary()+"\""+
                ", \"description\":\""+eventDTO.getDescription()+"\""+
                ", \"raised\":\""+eventDTO.getRaised()+"\""+
                ", \"issueType\":\""+eventDTO.getIssueType()+"\""+
                ", \"priority\":\""+eventDTO.getPriority()+"\""+
                ", \"issuesubType\":\""+eventDTO.getIssuesubType()+"\""+
                ", \"region\":\""+eventDTO.getRegion()+"\""+
                ", \"airport\":\""+eventDTO.getAirport()+"\""+"}";

        mvc.perform(put(BASE + UPDATE.replaceAll("\\{.*\\}", ""))
                .contentType(MediaType.APPLICATION_JSON)
                .content(json))
                .andExpect(status().isInternalServerError());

    }


(I have already created the object in the setup) The status i'm getting is the 405

It has to throw status 500

Aucun commentaire:

Enregistrer un commentaire