Thursday, January 15, 2009

Google Programmer Explains Why Static Methods Are Bad For Java Unit Testing

This article made me question my frequent use of static utility methods:

Here are a couple of the excerpts I liked from the article:

The basic issue with static methods is they are procedural code. I have no idea how to unit-test procedural code. Unit-testing assumes that I can instantiate a piece of my application in isolation. During the instantiation I wire the dependencies with mocks/friendlies which replace the real dependencies. With procedural programing there is nothing to “wire” since there are no objects, the code and data are separate.

Lets do a mental exercise. Suppose your application has nothing but static methods. (Yes, code like that is possible to write, it is called procedural programming.) Now imagine the call graph of that application. If you try to execute a leaf method, you will have no issue setting up its state, and asserting all of the corner cases. The reason is that a leaf method makes no further calls. As you move further away from the leaves and closer to the root main() method it will be harder and harder to set up the state in your test and harder to assert things. Many things will become impossible to assert. Your tests will get progressively larger. Once you reach the main() method you no longer have a unit-test (as your unit is the whole application) you now have a scenario test. Imagine that the application you are trying to test is a word processor. There is not much you can assert from the main method.

The Sunburned Surveyor


Larry D. Becker said...

I seems like more of a critique of procedural programming than static methods. No one wants to go back to the bad old days, but the excesses of object oriented programming are just as bad. Everyone has seen cases of "over design" of classes where they attempt to put in every possible feature. I suspect the answer, as usual, lies somewhere in the middle. Static methods have their place.

Eron Hossain said...

Java language was discovered by James Gosling of Sun Micro systems in 1991. Although C, C++ like programming languages were present in the market but due to fix platform constraint, web developers were unable to develop high end applications. See more programming assignment help

rose said...

Much thanks to you such a great amount for imparting this value capable substance to us. The idea taken here will be valuable for my future projects and i will clearly actualize them in my investigation. Continue blogging article this way.

Java Training in Chennai|
Java Training in Bangalore|
Java Training in Bangalore|

uma mahi said...

As a Java learner really this blog helps me to understand about the static method in Java. Keep update more about pros and cons about Java :). Java Training with placement in chennai

Anonymous said...

Much thanks to you such a great amount for imparting this value capable substance to us.

manual testing online training

Anexas Europe said...

It’s great to come across a blog every once in a while that isn’t the same out of date rehashed material. Fantastic read.

Digital Marketing Training in Mumbai

Six Sigma Training in Dubai

Six Sigma Abu Dhabi