Powermock – Briefly



Mock objects is a unit testing technique in which a actual code  is replaced by dummy implementations that emulate real code. This helps one to write unit tests targeting the functionality provided by the class under test.

But before delving inside powermock the first and foremost question that any naive programmer will have  Why use Mocking frameworks?What is the use of the mocking frameworks? Can’t we perform any unit testing without mocking ? Okay lets try to understand the concepts and then may be we will be able to answer the above questions.

Lets consider a use case.We have 2 java class files. class A and class B. Class A has dependency on another class (say B) which means that somewhere in class A we are calling a method of class B through its objects.This works fine when we use any unit testing framework like junit or testng. Because all we need to do is to create object of B and call its method.But if the class B is a component which not in our project module ,lets say some external component .Then are we blocked here? Because we can’t create object of class B then how can we perform the unit test? .So,in this case we have to make a mock of the class B which will simulate the  behaviour of class B and it acts like a place holder. So we need objects of class B we don’t need to create another one explicitly we just need to use the mock B.Yes of course we can do unit testing without any mocking frameworks and thats a pretty good thing actually.Because when we opt for mocking then the unit testing also becomes a pain. But when our classes have external dependencies which are not available at runtime then most probably creating the mock is the solution to perform any unit tests and proceed.

There are several mocking frameworks available for java platform. Some of the widely used are:-

In this post  we will focus on how to work with “Powermock” and its  different use-case implementations .

Powermock comes in 2 different flavors. One distribution is based on Easymock and another distribution is based on Mockito. I will use the one with Mockito distribution as i feel Mockito as a mock framework has several advantages over Easymock. Powermock is hosted on github .You can download Powermockito from the link https://github.com/jayway/powermock/wiki/Downloads
After downloading just put them in your class path.Thats all on how to set up Powermockito

Related Post

1 Comment

  1. download shadow fight 2 hack

    Thanks for the excellent info, it actually comes in handy.


Leave a Comment

Your email address will not be published. Required fields are marked *

Menu Title