O'Reilly logo

Instant Mock Testing with PowerMock by Deep Shah

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Mocking static methods (Simple)

The real power of PowerMock is the ability to mock things that other frameworks can't. One such thing is mocking static methods.

In this recipe we will see how easily we can mock static methods.

Getting ready

The use of static methods is usually considered a bad Object Oriented Programming practice, but if we end up in a project that uses a pattern such as active record (see http://en.wikipedia.org/wiki/Active_record_pattern), we will end up having a lot of static methods.

In such situations, we will need to write some unit tests and PowerMock could be quite handy.

Start your favorite IDE (which we set up in the Getting and installing PowerMock (Simple) recipe), and let's fire away.

How to do it...

  1. We will start where ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required