How can I make to throw this catch with JUnit

Terro :

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

This is the function that 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 and when i put an issueKey i got the status 200 (even when the issue key don't exist)

It has to throw status 500

PCaC :

I think that in the JUnit test you have to make .thenThrow(eywaException); instead of the .thenReturn(successResponseDTO); to make the test go through the exception and get the 500 status

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=165429&siteId=1