Partner – Moderne – NPI EA (cat=Spring Boot)
announcement - icon

Refactor Java code safely — and automatically — with OpenRewrite.

Refactoring big codebases by hand is slow, risky, and easy to put off. That’s where OpenRewrite comes in. The open-source framework for large-scale, automated code transformations helps teams modernize safely and consistently.

Each month, the creators and maintainers of OpenRewrite at Moderne run live, hands-on training sessions — one for newcomers and one for experienced users. You’ll see how recipes work, how to apply them across projects, and how to modernize code with confidence.

Join the next session, bring your questions, and learn how to automate the kind of work that usually eats your sprint time.

Course – Black Friday 2026 – NPI EA – main – before_post_content
announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 1st December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

Course – Black Friday 2026 – NPI – main – before_post_content
announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 1st December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

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.

Partner – Moderne – NPI EA (tag=Refactoring)
announcement - icon

Modern Java teams move fast — but codebases don’t always keep up. Frameworks change, dependencies drift, and tech debt builds until it starts to drag on delivery. OpenRewrite was built to fix that: an open-source refactoring engine that automates repetitive code changes while keeping developer intent intact.

The monthly training series, led by the creators and maintainers of OpenRewrite at Moderne, walks through real-world migrations and modernization patterns. Whether you’re new to recipes or ready to write your own, you’ll learn practical ways to refactor safely and at scale.

If you’ve ever wished refactoring felt as natural — and as fast — as writing code, this is a good place to start.

Course – Black Friday 2026 – NPI EA – main – after_post_content

announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 1st December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

Course – Black Friday 2026 – NPI – main – after_post_content

announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 1st December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW