单元测试高度相互依赖的代码
所以我有一些具有挑战性的代码想要重构。挑战在于它依赖于数据库查询、EJB 和 Java serverFaces。不是同时,而是接近。
一个很好的例子是 geocoder。根据输入和存储的数据对数据库进行多次查询,获得有意义的结果。该代码还可能引用其他帮助器类并通过 JSF 框架查找它们。
测试此类代码的最佳策略是什么?我应该尝试尽可能地分离我的代码吗?我应该使用嘲笑来代替吗?什么对其他人有用?
So I have some challenging code I would like to refactor. The challenge is that it depends on Database queries, EJB and Java serverFaces. Not simultaneously but close to it.
A good example would be a geocoder. Getting meaningful results depending on multiple queries to the DB depending on the data entered and stored. The code might also reference other helper classes and look them up via the JSF framework.
What are the best strategies for testing this sort of code? Should I try to separate out my code as much as possible? Should I use mocking instead? What has worked for other people?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
嗯,简短的回答是“是”。
首先,您需要充分考虑代码以构建单元测试。您所描述的内容对于应用通常的单元测试方法来说过于复杂,并且在任何情况下您都会得到的更像是更高级别的验收测试。
现在,就因式分解而言,您有几种可能的方法,并且您可能会全部使用它们。
使用外部脚本测试数据库查询本身。
为直接访问数据库的组件构建适当的模拟,以便了解已知结果会发生什么。
使用类似于 JUnit 的功能单元框架构建单元测试。
检查现有技术,看看是否可以根据单元测试有效地测试输出 HTML。
Well, the short answer is "yes".
You're going to need, first of all, to factor the code sufficiently to construct unit tests at all. What you're describing is excessively complicated to apply the usual unit test methods, and what you would get in any case is more like a higher-level acceptance test.
Now, as far as that factoring goes, you have several possible approaches, and you will probably use them all.
Test the data base queries themselves, using an external script.
Construct an appropriate mock for the components directly accessing the DB, in order to see what happens against known results.
Build unit tests using a JUnit like framework for units of functionality.
Examine the state of the art to see if you can usefully test the output HTML against unit tests.