Let's get started with a Microservice Architecture with Spring Cloud:
Mock Final Classes and Methods with Mockito
Last updated: August 13, 2025
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.
The code backing this article is available on GitHub. Once you're logged in as a Baeldung Pro Member, start learning and coding on the project.
















