为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

守着一只汪 2023-08-04 17:40:05
我有一个函数可以更新 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
查看完整描述

2 回答

?
慕斯王

TA贡献1864条经验 获得超2个赞

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



查看完整回答
反对 回复 2023-08-04
?
智慧大石

TA贡献1946条经验 获得超3个赞

我认为你的问题是路径变量。终点是“/update/{issueKey}”,但在测试过程中您调用“/update/”(?)。

Spring知道第一个端点,您调用第二个端点,并且Spring无法找到该端点并抛出405 Method Not allowed(您没有PUT / update)。


查看完整回答
反对 回复 2023-08-04
  • 2 回答
  • 0 关注
  • 90 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信