问题描述
我多年来一直是EasyMock的粉丝,多亏了SO,我遇到了对PowerMock的引用,并且它能够模拟构造函数和静态方法,这两种方法在将测试转换为遗留代码库时会导致问题。
I've been a fan of EasyMock for many years now, and thanks to SO I came across references to PowerMock and it's ability to mock Constructors and static methods, both of which cause problems when retrofitting tests to a legacy codebase.
显然,单元测试(和TDD)的一个巨大好处是它导致(强制?)更清洁的设计,在我看来, PowerMock的推出可能会减损它。我会看到这主要表现为:
Obviously one of the huge benefits of unit testing (and TDD) is the way it leads to (forces?) a much cleaner design, and it seems to me that the introduction of PowerMock may detract from that. I would see this mostly manifesting itself as:
- 回到初始化合作者而不是注入他们
- 使用静态而不是让方法归合作者所有
除此之外,有些东西并不适合我我的代码是为测试操作的字节码。我不能真正给出一个具体的理由,只是因为它让我感到有点不安,因为它只是为了测试而不是为了生产。
In addition to this, something doesn't quite sit right with me about my code being bytecode manipulated for the test. I can't really give a concrete reason for this, just that it makes me feel a little uneasy as it's just for the test and not for production.
在我目前我们真的推动单元测试作为人们改进编码实践的一种方式,感觉将PowerMock引入等式可能会让人们稍微跳过这一步,因此我不愿意开始使用它。话虽如此,我真的可以看到在哪里使用它可以减少开始测试一个类需要完成的重构量。
At my current gig we're really pushing for the unit tests as a way for people to improve their coding practices and it feels like introducing PowerMock into the equation may let people skip that step somewhat and so I'm loathe to start using it. Having said that, I can really see where making use of it can cut down on the amount of refactoring that needs to be done to start testing a class.
我想我的问题是,人们使用PowerMock(或任何其他类似的库)获得这些功能的经验是什么,你会使用它们吗?你希望你的测试总体上影响你的设计多少?
I guess my question is, what are peoples experiences of using PowerMock (or any other similar library) for these features, would you make use of them and how much overall do you want your tests influencing your design?
推荐答案
我认为你是对的。一旦你学会了如何,重构遗留代码是可测试的那么
I think you're right to be concerned. Refactoring legacy code to be testable isn't that hard in most cases once you've learned how.
最好慢一点,有一个支持性的学习环境,而不是快速学习和学习坏习惯。
Better to go a bit slower and have a supportive environment for learning than take a short cut and learn bad habits.
(我只是并认为它是相关的。)
(And I just read this and feel like it is relevant.)
这篇关于使用PowerMock或您的测试对您的设计有多大影响?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!