问题描述
我正在为Junits使用Mockito
I am using mockito for the Junits
我知道mockito不会模拟静态方法,但是有办法解决这个问题,而不是使用powermock吗?
I know mockito donot mock static methods but is there a way around for this instead of using powermock?
谢谢
推荐答案
可能的解决方法是将静态方法封装在实际实例中.还是静态方法背后的真实实例.
Possible workaround would be to encapsulate static methods in real instance I think. Or real instance behind the static method.
但这将意味着您必须修改生产代码.
Though that would mean you'll have to modify your production code.
老实说,如果您现在问自己这个问题,那么您在开发过程中测试代码就太迟了. (现在进行宣传;)如果您正在实践TDD,您可能会早点注意到此问题,并尽早调整设计以使其可以使用经典测试软件进行完全测试.
Honestly if you ask yourself this question now, you are testing your code too late in the development process. (Now evangelizing ;)) If you were practicing TDD, you would have noticed this issue early, and tweaked your design early to be fully testable with classic testing software.
在练习TDD时,我个人使用疼痛指标"来查看我的设计是否正确.当然,一切都取决于上下文,但这通常是良好设计的一个良好指标(至少对我和其他人而言).
I personally use the "pain metrics" when practicing TDD to see if my design is ok or not. Of course everything depends on the context, but usually this is a good indicator of good design (at least for me and some others).
因此,我的建议是摆脱这些静态方法或修改您的设计,使其不依赖于静态方法模拟.
So my advice get rid of these static methods or revise your design to not be dependent on static method mocking.
欢呼
这篇关于如何使用Mockito模拟静态方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!