Praise for PowerMock

Categories: Java

I’m generally a fan of unit-testing. I’m even quite happy to refactor existing code to make it test-friendly. However sometimes this is just not possible, either because it is third-party code, or because it has special performance or security constraints.

Having been a happy user of Mockito for quite a while, I’m therefore thrilled to have discovered PowerMock, a bytecode-weaving helper for Mockito (and EasyMock) that allows:

  • Creating mock objects for final classes;
  • Configuring mock responses for static methods;
  • Mocking responses to invocations of the new operator!

It all worked for me just as advertised - added the dependency to the maven pom, added two annotations (@RunWith and @PrepareForTest) to the test case class, and then used normal Mockito functionality plus a couple of PowerMockito.mock/PowerMockito.mockStatic/PowerMockito.mockNew calls to extend the existing test coverage to areas it previously could not reach.

Thanks guys!

comments powered by Disqus