春季启动集成测试,设置TestRestTemplate的请求属性

时间:2018-09-10 08:12:16

标签: spring-boot mocking integration-testing

我正在Id的预处理方法中设置名为RequestHandlerInterceptor的请求属性。我从jwt令牌获得了此ID。

 
 protected UserContext getCurrentUserContext() {
        return UserContextHolder.getCurrentContext();
 }

 @Override
 public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws AuthenticationException {
        UserProfile userProfile = null;
        try {
            UserContext userContext = getCurrentUserContext();
            userProfile = userContext.getUserProfile();         
            request.setAttribute("Id", String.valueOf(userProfile.getId()));
            return true;
        }

在我的控制器方法中使用@RequestAttribute来获取此属性。

@DeleteMapping("/{id}")
public void delete(@PathVariable Long id, @RequestAttribute("Id") String userId) throws EntityNotFoundException, ApiException {
        excessService.deleteExcess(id, userId); 
}

但是,尽管我已经覆盖了RequestHandlerInterceptor,但我的集成测试却失败了。

    requestHandlerInterceptor = new RequestHandlerInterceptor() {
        @Override
        public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws AuthenticationException {
            request.setAttribute("Id", "123456");
            return true;
        }
    } 

如何在集成测试中模拟此请求属性,以将“ id”值传递到控制器方法中?

0 个答案:

没有答案