我怎样才能用 JUnit 抛出这个 catch

我有一个函数可以更新 jira 中的问题,我想使用 JUnit 抛出捕获。


这是我得到的功能:


@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);

        }

    }


(我已经在设置中创建了对象)我得到的状态是 405,当我放置问题密钥时,我得到的状态是 200(即使问题密钥不存在)


它必须抛出状态 500


守着一只汪
浏览 92回答 2
2回答

慕斯王

我认为在 JUnit 测试中你必须 make.thenThrow(eywaException);而不是.thenReturn(successResponseDTO);使测试经历异常并获得 500 状态

智慧大石

我认为你的问题是路径变量。终点是“/update/{issueKey}”,但在测试过程中您调用“/update/”(?)。Spring知道第一个端点,您调用第二个端点,并且Spring无法找到该端点并抛出405 Method Not allowed(您没有PUT / update)。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java