Contents

How to Mock Final Classes and Final Methods in Java with Mockito

Updated on 2026-10-21

Introduction

This post explains how to mock final methods and classes. Mockito doesn't support this behavior by default. To stub final methods and mock final classes, we need to enable the inline mock maker first. When you try to mock a final class without the mockito inline mockmaker you get an error message like this: mockito cannot mock/spy because : - final class.

Maven Dependency

For the examples, I used version 4.2.0 of Mockito.

1
2
3
4
5
6
<dependency>
    <groupId>org.mockito</groupId>
    <artifactId>mockito-core</artifactId>
    <version>4.2.0</version>
    <scope>test</scope>
</dependency>

We also need the inline mock maker to stub final classes/methods. We can add a dependency or create a file inside the test resources to enable the new Mock maker.

Option 1: Inline mock maker dependency

Add this dependency in your pom.

1
2
3
4
5
6
<dependency>
    <groupId>org.mockito</groupId>
    <artifactId>mockito-inline</artifactId>
    <version>4.2.0</version>
    <scope>test</scope>
</dependency>

Option 2: Adding a file to enable the inline mock maker

Create a resources directory in your test directory if you do not have one already. Inside the resources create the directory mockito-extensions and in that directory the file org.mockito.plugins.MockMaker. The complete path with file should look like this: src/test/resources/mockito-extensions/org.mockito.plugins.MockMaker.

In the file org.mockito.plugins.MockMaker paste this text mock-maker-inline, now the mock maker is available during tests.

Mock final class

With inline mock maker, mocking a final class or method is the same as mocking a non-final class or method. Below we have an example that mocks the final class Cat. You can't tell from the @Test annotated method that we mock a final class.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
final class Cat {
    String makeSound() {return "Meow";}
}

@Test
void mockingFinalClass() {
    Cat cat = mock(Cat.class);
    when(cat.makeSound()).thenReturn("purr");
    assertThat(cat.makeSound()).isEqualTo("purr");
}

Mock final method

With inline mock maker enabled, mocking final methods also doesn't look any different from mocking a non-final method.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
class Bird {
    final String makeSound() {return "chirp";}
}

@Test
void mockFinalMethod(){
    Bird bird = mock(Bird.class);
    when(bird.makeSound()).thenReturn("tweet tweet");
    assertThat(bird.makeSound()).isEqualTo("tweet tweet");
}

Conclusion

In this post, You enabled the inline mock maker to mock final methods and classes. We also saw two examples of how to mock final classes and methods. Mocking a final class or method looks the same as mocking non-final classes and methods. Using this mock maker will prevent the mockito cannot mock/spy because - final class exception from happening again.

If you're curious and want to know more about what you can do with Mockito, please check out their documentation. It lists all the Mockito features and how to use them.

Further reading

More about testing in Java:

✅ In-House Team Workshop · 1 Day (Live In-Person On-Site)

Level Up Your Team's Java Testing: JUnit 5, Mockito & Testcontainers

Slow, flaky, or brittle test suites kill developer velocity. In my 1-day Modern Testing in Java Workshop, your team learns how to design fast, maintainable unit and microservice integration tests using advanced JUnit 5, Mockito, and Testcontainers on your real codebase.

Stay Connected · Code, Benchmarks & Talks

Want More Deep-Dive JVM Benchmarks & Project Panama Research?

Follow my weekly engineering posts on LinkedIn, explore the full JMH benchmark suites and pure-Java Linux io_uring / FUSE projects on GitHub, or download my conference talk slides:

David Vlijmincx
David Vlijmincx

Senior Java Performance & Concurrency Engineer with 10+ years of production JVM experience. International speaker with 35+ talks at Devoxx, P99 CONF, JFokus, JavaZone, and Devnexus, creator of JUring (Linux io_uring for Java), and enterprise Java workshop trainer.