When you have static code that gives you problems in your unit tests; so that you feel that you need to "mock", you have these options:
- You go to PowerMock (ito). It works great.
- You are accessing JMockit. Works well too.
- If you are testing code that you yourself wrote, you may need to step back and ask yourself: "Why did I write code that is now difficult for me to find unit test?"
In other words: if you want to use a fake framework, you must use one of the above. On the one hand, this is absolutely true. static is one part of the Java language; so why not use a framework that allows you to handle this?
But, of course, you still have a static call in your production code. Leading to a tight bond and preventing polymorphism.
So: if you can get rid of the static call (even when using the workaround suggested in another answer), all the better. If not: Mokito cannot help; you will need the magic of manipulating byte code or. JVM agents.
source share