Mocking static methods: the road to Hell...
http://www.giorgiosironi.com/2009/11/mocking-static-methods-road-to-hell.html
Static Methods are Death to Testability
http://googletesting.blogspot.com/2008/12/static-methods-are-death-to-testability.html
powermock(mock static方法的工具)
http://code.google.com/p/powermock/
上面两篇文章的思想都是从不要出现static方法这个思路看待这个问题的。认为静态方法是过程化代码而非面向对象的设计,认为静态类与调用代码耦合在一起,。应该通过对象的方式进行方法的调用。
我们的项目目前也是经常使用static方法,不过因为不方便mock测试,新写的code都是实例化后的了。如果使用Spring,使用起来非常容易,否则还需要new Instance();
powermock提供的工具不错,比如我希望mock System.currentTimeMillis()的时候,就派上用处了,Java语言本身也充斥着大量的过程化语言,在保证良好的系统结构前提下,我觉得不是什么问题。
-- heipark