Question

The problem is next: I need to test method methodTest() of private class. The methodTest() call static method staticExternal() from external library. How to replace the staticExternal() on a mockLocal(), if I use JMock and EasyMock, but class that contains the staticExternal() haven't interface?

P.S. PowerMock can not be used.

Was it helpful?

Solution

You can't, as far as I'm aware. Basically that code is hard to test. You should consider extracting a dependency, where the "normal" implementation of that dependency calls the static method.

Fundamentally, static method calls are hard to replace precisely because they're static. Tools like PowerMock have to furtle with the guts of Java (replacing classloaders etc) in order to intercept static method calls. In my opinion it's usually better to refactor your code so that it's testable without that sort of thing.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top