Thinking Twice Before Using @MockBean
号外!号外!号外!你的 spring boot integration tests 运行慢吗,是不是每跑一次测试,你都在等待,等待它全绿的那一瞬间。假如你遇到,那请接着往下看,也许可以帮助到你。假如你没有遇到,那也请往下看,由于也许以后你会遇到。
告诉你一个秘密:@MockBean会导致测试类(Test Class)之间spring boot application context不断启动屡次!!!
不信,那么我们请看栗子 MockBean Annotation:
这项目有两个测试类,AboutControllerTest 和 TokenControllerTest
AccountControllerTest:
account-controller.png
TokenControllerTest:
token-controller.png
AccountControllerTest 没有用 @MockBean,TokenControllerTest 用 @MockBean。下面是两个测试一起运行产生的日志:
spring-boot-log.png
如上图所示,spring boot 启动两次。而spring boot 的启动时间也比较耗时,所以@MockBean,很有可能导致测试运行的很慢。那@MockBean究竟是个怎样样的存在?
WHAT
写过spring boot integration test的小伙伴,对于@MockBean应该会比较熟习。在写测试时,对于少量应使用的外部依赖需要进行少量Mock 解决,比方:Redis、ElasticSearch、ExternalService 等。官方文档详情 Mocking and spying beans:
mock-bean.png
- It allows to add Mockito mocks in a Spring ApplicationContext.
- If a bean, compatible with the declared class exists in the context, it replaces it by the mock.
- If it is not the case, it adds the mock in the context as a bean.
也就是说,@MockBean会改变spring boot application context beans,导致用了@MockBean的测试类之间的需要不同application context,从而导致spring boot application context重启。为什么需要不同application context 就需要重启???带着疑惑,我们接着往下看。
WHY
Spring Boot Application Context
什么是application context?简单了解,就是应使用程序运行所需要的上下文。官方文档详情 Context Management:
context-management.png
官方文档详情 Context management and caching:
testing-ctx-management.png
根据官方文档意思,application context为初始化测试实例提供上下文,假如需要不同的application context实例化不同的测试,就需要重新启动spring boot,创立不同applicaiton context。文档还说到,为理解决spring boot application context启动慢的问题,会做缓存解决。那@MockBean究竟破坏了什么样的缓存规则,从而导致spring boot重启屡次?是什么导致打开方式出了问题?
Spring Boot Application Context Cache
要答复这个问题,就要先理解[application context caching]的uniquely key包含的内容,附上官方文档详情 Context caching:
context-caching.png
根据文档的形容,不难知道application context cacheing是通过key:value方式进行缓存的,唯一键为组合键,包含:locations、classes、contextInitializerClasses、contextCustomizers、contextLoader、parent、activeProfiles、propertySourceLocations、propertySourceProperties、resourceBasePath。
而@MockBean的用会导致每个application context中contextCustomizer的不同,从而导致存储在context cache中的application context的uniquely key不同,最终导致application context在测试类之间不能共享。尽管没有官方文档说明这一点,不过在
org.springframework.boot.test.mock.mockito.MockitoContextCustomizerFactory 源代码中可以找到少量痕迹:
mock-customizer.png
图中所说的MergedContextConfiguration就是application context caching的uniquely key。
HOW
对于spring boot integration test 来说,除了 external service(clients...) 需要被 Mock,其它的内部依赖(service、repository…)都不应该被Mock。external service 可以在配置层,进行Mock,而后在测试类中,直接通过@Auotwrite方式注入:
RedisTemplateBeanConfigurationMocker:
redis-configuration-mocker.png
RedisConfiguration:
redis-configuration.png
TokenControllerTest:
token-controller-fixed.png
TokenControllerTest,直接 @Autowrite RedisTemplateBeanConfigurationMocker 中配置的,RedisTemplate @Bean。完成栗子,请查mockbean-annotation。
写在最后
spring boot integration test 相对于 api test,应该更关注api功能的完整性,理解依赖的边界,不需要Mock的,就不要Mock,比方:service, repository…。对于外部依赖,统一在配置层完成 Mock,比方:client、redis、rabbitmq...。
原文链接
1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长!
2. 分享目的仅供大家学习和交流,您必须在下载后24小时内删除!
3. 不得使用于非法商业用途,不得违反国家法律。否则后果自负!
4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解!
5. 如有链接无法下载、失效或广告,请联系管理员处理!
6. 本站资源售价只是摆设,本站源码仅提供给会员学习使用!
7. 如遇到加密压缩包,请使用360解压,如遇到无法解压的请联系管理员
开心源码网 » Thinking Twice Before Using @MockBean
account-controller.png
token-controller.png
spring-boot-log.png
mock-bean.png
context-management.png
testing-ctx-management.png
context-caching.png
mock-customizer.png
redis-configuration-mocker.png
redis-configuration.png
token-controller-fixed.png