0

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.

blackhard
  • 502
  • 7
  • 26
  • possibly this question is already answered in [these](http://stackoverflow.com/questions/3162551/how-do-i-mock-static-methods-in-a-class-with-easymock) [two](http://stackoverflow.com/questions/218813/jmock-mocking-a-static-method) – default locale Mar 06 '13 at 14:15
  • Why are you rejecting PowerMock, when it's the simple and correct solution to the problem you have? If you want a constrained answer, you'll have to explain why. – Andrew Aylett Mar 06 '13 at 17:24
  • @AndrewAylett I know that PowerMock is good framework. Unfortunately, I have such exercise. Thanks all! – blackhard Mar 06 '13 at 17:54
  • use power mock http://www.michaelminella.com/testing/how-to-mock-static-methods.html – Bala Jul 31 '13 at 13:49

1 Answers1

3

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.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194