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 .
- Spring Boot – University Book Keeping App , Part – 2 - January 14, 2017
- Spring Boot – University Book Keeping App , Part – 1 - January 14, 2017
- Spring Boot – University Book Keeping App - January 14, 2017
- A Shoot at Permgen & Metaspace - January 14, 2017
- Spark – Shell - January 2, 2017
- Spark – Concepts - January 2, 2017
- Spark – Sample Application in Windows - December 31, 2016
- MyBatis – Components - June 17, 2016
- Castor – Default Mapping - June 17, 2016