Spying or Mocking Abstract Classes
Abstract classes are referred to as the class declared with the abstract keyword that may or may not include the abstract methods. In Java, abstract classes cannot be instantiated, but they can be sub-classed. It can also have static fields and static methods.
In this section, we will discuss mocking or spying of abstract classes. We will analyze several cases of testing the abstract classes using non-abstract methods.
For spying or mocking the abstract classes, we need to add the following Maven dependencies:
All the required dependencies of the project are given below:
The PowerMock dependencies are only required for the test in which we make use of PowerMock.
Examples of Mocking Abstract Class
1. Spying abstract class using Mockito.spy()
In this example, we are going to spy the abstract classes using the Mockito.spy() method. The Mockito.spy() method is used to create a spy instance of the abstract class.
Step 1: Create an abstract class named Abstract1_class that contains both abstract and non-abstract methods.
Step 2: Create a JUnit test case named Abstract1Test. It contains a spy instance of the abstract class.
The following output shows that the test is successfully running.
The drawback of using the Mockito.spy() method is that it will invoke the abstract class constructor during the creation of spy instance. In most of the cases, the constructor uses external dependencies that can be an obstacle to our unit test executions. These external dependencies are usually known as the test impediments. It is the reason to use Mockito.mock() method for mocking abstract classes.
2. Mocking abstract class using Mockito.mock()
In this example, we are going to mock the abstract classes using the Mockito.mock() method.
Usually, mocking is used to create a clone or dummy object of the class. In other words, it makes a class vacant from its logic or algorithms. The created mock instance does not contain code (logic) inside the methods.
Step 1: Create an abstract class named Abstract_Class that contains both abstract and non- abstract methods.
Step 2: Create a JUnit test case named AbstractTestClass for mocking the abstract class.
In the above code, ac is a mocked instance created using Mockito.mock() method.
The following output shows that the test is successfully running using Mockito.
The above approach is not the best, but it can be used. The next approach is recommended because it uses PowerMock, and can have control over the private methods defined in the abstract classes.
3. Mocking abstract class using PowerMock
In the following example, we will use PowerMockito.mock() method for mocking the abstract classes. Using PowerMock instead of Mockito.mock() is a better approach as it can have control over the private as well as static methods.
Step1: Create an abstract class named Abstract_class that contains both abstract and non-abstract methods.
Step 2: Create a JUnit test case named AbstractTestClass for testing purposes.
Here, we have asked the PowerMock to stub the private method's return values so that we can test the sayMock() method without any test impediments. Mockito alone cannot stub this method, that is why we have used PowerMock along with Mockito.
The following output shows that the test is successfully running using PowerMock with Mockito.