by bglazer · 11y ago · 0 comments
> Look at Stack-Overflow ... they also use only static methods for performance, we should do the same ... Gordy had dismissed Ina’s love of unit testing. It’s hard to unit test code written mostly with static methods.
I also happen to like static methods quite a bit, precisely because I thought they were EASY to unit test. I thought that a static method, which is essentially just a function in the enclosing object's namespace, would be EASY to unit test. Input -> output. No state.
Am I digging my own code grave here? What am I missing?
public static doSomething() {
Database.write("Did something")
}
Makes testing hard because Database is hardcoded.In short: static methods are easy to test, methods using static methods (whether static or not) are more difficult to test.
My takeaway: the bar for moving functionality into non-static methods (which will still be decently easy to test) is quite low. If you call a static method, be prepared to own all its functionality when testing the calling method. Taken to logical conclusion, public static methods should be quite small.
It can be a useful tool, but it causes all kinds of weirdness which can completely mitigate any advantages. On my current project I had to strip out most of our uses of PowerMock. While the tests pass on my local machine just fine, many of them fail for no good reason on the CI server.
Offtopic: Time for start using Docker? :p
public static doSomething(Database db){
db.write("did something"
}