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).
这篇关于在集成测试中进行模拟是否被认为是一种好习惯?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在集成测试中进行模拟是否被认为是一种好习惯
基础教程推荐
- Java:带有char数组的println给出乱码 2022-01-01
- 设置 bean 时出现 Nullpointerexception 2022-01-01
- Java Keytool 导入证书后出错,"keytool error: java.io.FileNotFoundException &拒绝访问" 2022-01-01
- 如何使用 Java 创建 X509 证书? 2022-01-01
- “未找到匹配项"使用 matcher 的 group 方法时 2022-01-01
- 降序排序:Java Map 2022-01-01
- 减少 JVM 暂停时间 >1 秒使用 UseConcMarkSweepGC 2022-01-01
- 在 Libgdx 中处理屏幕的正确方法 2022-01-01
- 无法使用修饰符“public final"访问 java.util.Ha 2022-01-01
- FirebaseListAdapter 不推送聊天应用程序的单个项目 - Firebase-Ui 3.1 2022-01-01