我正在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”值传递到控制器方法中?