Is it in considered a good practice to mock in integration test?(在集成测试中进行模拟是否被认为是一种好习惯?)
问题描述
有人告诉我@Mock 通常只用于单元测试,但我认为它对于替换测试类之外的外部部分很有用.在集成测试中模拟是否正确?
Somebody told me @Mock is usually employed only for unit tests but I think is useful for substituting the external parts outside the tested class. Is it correct to mock in integration tests?
推荐答案
最后,这都是关于措辞的.
In the end, this is all about wording.
当您从最基本的意义上考虑正确"时,例如 正确性,那么答案很简单:没有.
When you think of "correct" in its very fundamental sense, as in correctness, then the answer is simply: no.
您知道,集成测试的目标是确保您的集成系统(由多个不同的组件组成)按预期运行.集成测试的目的是验证您的组件管道"是否按预期工作.因此:当系统的部分被模拟出来时,您无法验证您的系统是否正常工作.
You see, the goal of an integration test is to ensure that your integrated system (consisting of multiple, different components) functions as expected. The purpose of an integration test is to verify that your "plumbing" of components works as expected. Therefore: you can't verify that your system works when parts of that system are mocked out.
但是,您可以不那么严格地考虑正确性".
But then, you can think "correctness" less strictly.
示例:销售汽车的公司必须测试 ECU.基本上是一个硬件,运行一个潜在的巨大软件堆栈.这些 ECU 通常在 汽车 内运行.所以当你想对一个ECU进行集成测试时,你必须把ECU放到汽车中进行测试,对吧?一辆可能还不存在的汽车.这里的解决方案:有硬件模拟器.您将 ECU 插入该仿真器,然后 ECU 会思考"位于真车内的内容.
Example: companies selling cars have to test ECUs. Basically a piece of hardware, running a potentially huge software stack. These ECUs normally operate within cars. So when you want to integration test an ECU, you would have to put the ECU into a car for testing, right? A car that probably doesn't exist yet. The solution here: there are hardware emulators. You plug the ECU into that emulator, and the ECU "thinks" that sits inside a real car.
所以:有很好的论据声称真正的集成测试不能使用模拟",但与此同时,在现实世界中,这种模拟"一直在发生.
So: there are good arguments to claim "a true integration test can't use mocking", but at the same time, in the real world, such "mocking" happens all the time.
因此,真正的答案是:这取决于上下文.因此,没有一个普遍的答案.相反,这是关于沟通.您只需"确保您的组/组织中的所有人都对这些术语有相同的理解.
The real answer is therefore: it depends on context. Therefore there isn't a universal answer. Instead, this is about communication. You "simply" have to ensure that all people in your group/org have the same understanding of such terms.
这个词本身可以用不同的方式来解释.您(共同!)选择最适合您需求的定义,然后确保所有为您的项目做出贡献的人都共享该观点(或至少了解它).
The term itself can be interpreted in different ways. You (jointly!) pick that definition that best fits your needs, to then make sure all people contributing to your project share that view (or at least know about it).
这篇关于在集成测试中进行模拟是否被认为是一种好习惯?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在集成测试中进行模拟是否被认为是一种好习惯
- Spring Boot连接到使用仲裁器运行的MongoDB副本集 2022-01-01
- Safepoint+stats 日志,输出 JDK12 中没有 vmop 操作 2022-01-01
- Java包名称中单词分隔符的约定是什么? 2022-01-01
- Eclipse 插件更新错误日志在哪里? 2022-01-01
- Jersey REST 客户端:发布多部分数据 2022-01-01
- C++ 和 Java 进程之间的共享内存 2022-01-01
- value & 是什么意思?0xff 在 Java 中做什么? 2022-01-01
- 从 finally 块返回时 Java 的奇怪行为 2022-01-01
- 如何使用WebFilter实现授权头检查 2022-01-01
- 将log4j 1.2配置转换为log4j 2配置 2022-01-01