1. Overview
In this short tutorial, weβll learn how to mock final classes and methods using Mockito. Also, if we use an older version of Mockito (pre-2.x), weβll see how we can use PowerMock.
As with other articles focused on the Mockito framework (such as Mockito Verify, Mockito When/Then and Mockitoβs Mock Methods), weβll use the MyList class shown below as the collaborator in test cases.
Weβll add a new method for this tutorial:
public class MyList extends AbstractList<String> {
final public int finalMethod() {
return 0;
}
}
And weβll also extend it with a final subclass:
public final class FinalList extends MyList {
@Override
public int size() {
return 1;
}
}
2. Using Mockito
In this section, weβll use the Mockito version 2.x or above to mock a final method and class.
2.1. Maven Dependencies
Letβs add the mockito-core and mockito-junit-jupiter dependencies to our pom.xml:
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-junit-jupiter</artifactId>
<version>5.12.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>3.3.0</version>
<scope>test</scope>
</dependency>
2.2. Mock a Final Method
Once weβve properly configured Mockito, we can mock a final method like any other:
@Test
public void whenMockFinalMethod_thenMockWorks() {
MyList mock = mock(MyList.class);
when(mock.finalMethod()).thenReturn(1);
assertThat(mock.finalMethod()).isNotZero();
}
By creating a concrete instance and a mock instance of MyList, we can compare the values returned by both versions of finalMethod() and verify that the mock is called.
2.3. Mock a Final Class
Mocking a final class is just as easy as mocking any other class:
@Test
public void whenMockFinalClass_thenMockWorks() {
FinalList mock = mock(FinalList.class);
when(mock.size()).thenReturn(2);
assertThat(mock.size()).isNotEqualTo(1);
}
Similar to the test above, we create a concrete instance and a mock instance of our final class, mock a method and verify that the mocked instance behaves differently.
3. Using PowerMock
PowerMock can mock final classes and methods, which Mockito (before version 2.x) cannot do.
3.1. Maven Dependencies
To use PowerMock, we need to add the powermock-module-junit4 and powermock-api-mockito2 dependencies to our pom.xml:
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-module-junit4</artifactId>
<version>2.0.9</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-api-mockito2</artifactId>
<version>2.0.9</version>
<scope>test</scope>
</dependency>
3.2. Mock a Final Method
Letβs mock the final method of the MyList class:
@RunWith(PowerMockRunner.class)
@PrepareForTest(MyList.class)
public class PowerMockFinalsUnitTest {
@Test
public void whenMockFinalMethod_thenMockWorks() throws Exception {
MyList mockClass = PowerMockito.mock(MyList.class);
when(mockClass.finalMethod()).thenReturn(1);
assertThat(mockClass.finalMethod()).isNotZero();
}
}
The test class PowerMockFinalsUnitTest is annotated with @RunWith(PowerMockRunner.class) to enable PowerMockβs custom test runner and @PrepareForTest(MyList.class) to prepare the MyList class for mocking.
3.3. Mock a Final Class
Mocking a final class using PowerMock is similar to Mockitoβs approach:
@RunWith(PowerMockRunner.class)
@PrepareForTest(FinalList.class)
public class PowerMockFinalsUnitTest {
@Test
public void whenMockFinalClass_thenMockWorks() throws Exception {
FinalList mockClass = PowerMockito.mock(FinalList.class);
when(mockClass.size()).thenReturn(2);
assertThat(mockClass.size()).isNotEqualTo(1);
}
}
4. Conclusion
In this quick article, we discussed how to mock final classes and methods with Mockito by using a Mockito extension and PowerMock.
