3

I'm using JUnit with Mockito. PowerMock can mock static methods but it doesn't seem to be possible to use it with Java 1.4, specially since it needs annotations.

Is there any other alternative?

Thanks.

3 Answers3

1

Personally, I prefer minimizing my use of mocks. If the static method is in your code, I'd modify it to make it more unit test friendly. Maybe it shouldn't be static. Or if it has to be, maybe you could use a setup method that determines how the static method behaves.

If the static method is not in your code, you're probably out of luck.

Adam Crume
  • 15,614
  • 8
  • 46
  • 50
  • I personally favor the use of mocks so to be able to make true unit tests. I agree it shouldn't be static, but unfortunately I can't change it for the moment. – Nicolas Font Mar 09 '11 at 17:12
1

(Shameless self promotion here) There is a project PowerMock-Legacy that lets you use PowerMock in Java 1.4. It is a bit verbose, and not all functionality is supported, but may worth a try.

Pablo Grisafi
  • 5,039
  • 1
  • 19
  • 29
  • Looks good. Even though I'd like to not need to mock static methods, that's probably the best answer to the given question. – Nicolas Font Mar 09 '11 at 17:19
0

Try jMockit and double check if you can refactor the code (or is it legacy code?). Mockito says about mocking static Methods

Mockito prefers object orientation and dependency injection over static, procedural code that is hard to understand & change. (Source)

Check also this related post especially Jon's answer.

Community
  • 1
  • 1
Martin Dürrmeier
  • 1,653
  • 5
  • 18
  • 35
  • Unfortunately it is legacy code I can't change for the moment. But I agree, the long term goal is to not use static methods at all. – Nicolas Font Mar 09 '11 at 17:07