We have all read it or heard someone talk about panduan bermain forex. Static Methods are Death to Testability”. As you can see, it returns the human readable version of the machine name.
Now there is an issue with this. So the requirements for our use of the above utility class is to create a URL to an image hosted on the local machine. Now you can stop laughing at the example. The purpose of this tutorial is not to find some painful testing feature, it’s to show where the ability to mock static method calls is useful. To record our expectations we do exactly as we normally would.
Instead of listing out all of the mock objects we want to replay, this method handles it for us. The above code is all you need to test those “nasty” static methods. I never said it would be pretty. But what it does do is it implements the test correctly and demonstrates my point.
Just because you can’t do something with your current toolset doesn’t mean that it’s bad. It just means you may need to re-evaluate your toolset. As always, I’m excited to read your comments! What about other ways of addressing the same issue? This is why I don’t like powermock. It screws with the class loader and using pertest is NOT a good option.
Do I still use static methods? Powermock is great for testing legacy code or stuff you don’t have control over, but I think it doesn’t force one to think about design the same way that standard Mock APIs do. It should be used only when absolutely necessary, not by default. I agree with you in regards to evaluating the use of static methods, however they are a tool just like any other feature of the Java language.
I’ve blogged about JMockit before and have a tutorial on this site about it. When your test frameworks don’t play nicely with implementations that use static methods, I don’t think that you should adopt frameworks that use complex class-loader manipulations so deterministically. Thanks for taking the time to share your view with us. How do I test static methods that do not return anything?
To clarify, how do I mock a DEPENDENT class that has a static method with void return type? Man, I couldn’t agree with this more. I’m so tired of listening to people try to bend the rules of good design around their tools. There are plenty of cases where using static methods is a good clean design. Of course they can be abused, but so can anything. The point of good design is making the right choices for the circumstances not mantras like “x is always bad”.