Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Static method calls in Java code is a nightmare when testing.


Why? Especially if we're talking stateless methods?


Am I missing something? I regularly write static methods and test them with JUnit.


> I regularly write static methods and test them with JUnit

Implementing mock objects which return "unexpected" results is impossible with static methods.

The standard 1 interface + 1 impl pattern in Java is just so that the Proxy.newProxyInstance can create a decorated or mocked object for testing.

So you can test the methods directly, but you can't write failure-inducing methods (like a connect exception throwing one) which test the methods which use it.


Are they any different in other languages? What's special about Java?




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: