spring boot http接口单元测试运行流程分析
文章目录
先粘贴一下test方法截图
测试类启动过程:







HandlerMappingIntrospector的initHandlerMappings()具体逻辑如下:
private static List<HandlerMapping> initHandlerMappings(ApplicationContext applicationContext) {
Map<String, HandlerMapping> beans = BeanFactoryUtils.beansOfTypeIncludingAncestors(
applicationContext, HandlerMapping.class, true, false);
if (!beans.isEmpty()) {
List<HandlerMapping> mappings = new ArrayList<>(beans.values());
AnnotationAwareOrderComparator.sort(mappings);
return Collections.unmodifiableList(mappings);
}
return Collections.unmodifiableList(initFallback(applicationContext));
}
可以看到beans的数量为8,此时HandlerMappingIntrospector.handlerMappings属性值已经被赋值了。
private void filterAndRecordMetrics(HttpServletRequest request,
HttpServletResponse response, FilterChain filterChain)
throws IOException, ServletException {
Object handler;
try {
handler = getHandler(request);//1
}
catch (Exception ex) {
logger.debug("Unable to time request", ex);
filterChain.doFilter(request, response);
return;
}
//
filterAndRecordMetrics(request, response, filterChain, handler);
}第1处获取HandlerMethod;第2处,继续执行doFilter()方法,最终在MockFilterChain.doFilter()执行到DispatcherServlet.doDispatch(request, response);
其中mappedHandler = getHandler(processedRequest);获取HandlerExecutionChain。
Determine handler adapter for the current request.
HandlerAdapter ha = getHandlerAdapter(mappedHandler.getHandler());在上面codwe块中获取合适的HandlerAdapter。
中根据mappedHandler得到HandlerMethod
中依据HandlerMethod利用反射技术去调用目标类,获得返回值。

评论
登录后参与技术讨论,分享你的经验与见解。
登录 / 注册暂无评论