@SpringBootTest 原理解析
2022-12-20 21:29:46 0 举报
AI智能生成
@SpringBootTest 原理解析
作者其他创作
大纲/内容
扩展类 SpringExtension
1. beforeAll() 方法
1. 创建了 TestContextManager 对象
createBootstrapContext 创建了applicationContext
DefaultCacheAwareContextLoaderDelegate#loadContextInternal
<font color="#ff00ff">SpringBootContextLoader#loadContext (重要)</font>
2. 加载 TestExecutionListeners (两种策略)
1. 从测试类上加载注解 @TestExecutionListeners
2. 加载默认的 spring.factories
3. 执行 TestExecutionListener#beforeTestClass 回调方法
2. postProcessTestInstance() 方法
1. 执行 TestExecutionListener#prepareTestInstance回调方法
3. beforeEach() 方法
1. 执行 TestExecutionListener#beforeTestMethod 回调方法
4. beforeTestExecution() 方法
1. 执行 TestExecutionListener#beforeTestMethod 回调方法
5. afterTestExecution() 方法
1. 执行 TestExecutionListener#afterTestExecution 回调方法
6. afterEach() 方法
1. 执行 TestExecutionListener#afterTestMethod 回调方法
7. afterAll() 方法
1. 执行 TestExecutionListener#afterTestClass 回调方法
重要的 TestExecutionListener 类
1. DependencyInjectionTestExecutionListener
2. MockitoTestExecutionListener
prepareTestInstance 初始化注入bean
<font color="#ff00ff">beforeTestMethod 重新注入bean 非常重要</font>
3. EventPublishingTestExecutionListener
重要的 ContextCustomizerFactory 类
MockitoContextCustomizerFactory
DefinitionsParser parser = new DefinitionsParser()
parseDefinitions(testClass, parse) 解析testClass中的mockBean
<font color="#00ff00">MockitoContextCustomizer</font>
注册 MockitoPostProcessor
重要的 BeanPostProcessor
MockitoPostProcessor
postProcessBeanFactory
解析配置类
注册 MockBean 和 SpyBean,替换原有的bean
<font color="#ffff00">再次获取时, bean 就是这个 MockBean 了</font>
postProcessProperties
对所有的 bean 注入 mockBean
<font color="#ff00ff">inject (最核心的方法)</font>
判断 beanNameRegistry 中是否有注册过 mockBean 和 spyBean
获取 Filed 的对象 和 beanFactory 中的 对象,判断是否一致
0 条评论
下一页