Quick revision
The short answer
Classic Mockito creates a subclass of the mocked type and overrides its methods, which cannot work for final classes or static methods. The inline mock maker instead uses a Java agent, attached through Byte Buddy, to retransform the bytecode of the real class at runtime so that each method first asks Mockito whether a mock should answer. Recent JDKs warn when such an agent is loaded dynamically and plan to disallow it by default, so Mockito now recommends attaching it with -javaagent in the test JVM, for example through Surefire's argLine.
What to remember
In this answer, we saw that subclass-based mocking cannot reach final classes or static methods, so Mockito's inline mock maker uses a Byte Buddy agent to retransform real classes, inserting a check at the start of each method that hands control to Mockito for registered mocks. JDK 21 started warning about dynamically loaded agents and plans to disallow them by default, so we should start the test JVM with -javaagent pointing at mockito-core, for example through Surefire's argLine. Some JDK types still cannot be mocked.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
Classic Mockito creates a subclass of the mocked type and overrides its methods, which cannot work for final classes or static methods. The inline mock maker instead uses a Java agent, attached through Byte Buddy, to retransform the bytecode of the real class at runtime so that each method first asks Mockito whether a mock should answer. Recent JDKs warn when such an agent is loaded dynamically and plan to disallow it by default, so Mockito now recommends attaching it with -javaagent in the test JVM, for example through Surefire's argLine.
Why this question is asked
Most of us use mockStatic() or mock a final class without thinking about how Mockito pulls it off. Interviewers at the senior level ask this because the answer explains a whole family of practical problems: warnings in the build log after a JDK upgrade, classes that refuse to be mocked, and test suites that need new JVM flags. In this answer, we will recap how subclass mocking works and where it stops, see what the inline mock maker does with a Java agent, look at the JDK change around dynamic agent loading, configure the agent properly, and finish with the limits that remain.
1A Quick Recap: Mocking by Subclassing
For most of its history, Mockito created a mock by generating a new class at runtime that extends the mocked class or implements the mocked interface. Every method is overridden to call Mockito's handler, which records the call and returns the stubbed answer. Byte Buddy, a library for generating and changing bytecode, builds that subclass.
This approach is simple and safe, because the real class is never touched. But it depends on Java's rules for subclassing. A final class cannot be extended, a final method cannot be overridden, and a static method belongs to the class itself, so no subclass can replace it. For those cases the subclass mock maker has no way in.
So to mock finals and statics, Mockito had to change the real class instead of extending it. That is what the inline mock maker does, and it is the default since Mockito 5.
2How the Inline Mock Maker Changes the Real Class
The JVM has an instrumentation API, java.lang.instrument, that lets a Java agent rewrite the bytecode of classes, even classes that are already loaded. This is called retransformation. Byte Buddy can attach Mockito's agent to the running JVM, and then Mockito asks the JVM to retransform the class we want to mock.
There is an important restriction: retransformation may change method bodies, but it may not add fields or methods or change the class hierarchy of a loaded class. So Mockito cannot simply add a 'mock handler' field. Instead, it inserts a small piece of code at the start of each method body. When the method runs, that code checks whether this object (or, for statics, this class on this thread) is registered as a mock. If it is, Mockito's handler answers and the original body is skipped. If not, the original code runs as before.
Notice what this means: after a class has been instrumented, real instances of it carry the extra check too, but behave normally because they are not registered. Mock instances are plain objects of the real class that Mockito keeps track of, often through a weak map, so a 'mock' of a final class is truly an instance of that final class.
7 messages in order. Play them, or step through them one at a time.
The real class is changed once; registration decides whether a call is answered by Mockito.
3What Changed in Recent JDKs
Attaching an agent to a JVM that is already running is called dynamic agent loading. It is powerful, which is also why the JDK team wants it to be visible. JEP 451, delivered in JDK 21, makes the JVM print a warning when an agent is loaded dynamically, and states the plan that a future release will disallow it by default, unless the JVM is started with -XX:+EnableDynamicAgentLoading. The goal is integrity: code should not be able to rewrite the platform silently.
Mockito's inline mock maker is exactly such a dynamically loaded agent, so on JDK 21 and later a test run that mocks a final class or a static prints a warning similar to the one below. Today it is only a warning. The risk is that a future JDK turns it into a failure.
The exact wording depends on the JDK build, so check your version. The meaning is stable: the agent should be declared when the JVM starts, not attached later.
4Configuring Mockito as a -javaagent
The fix Mockito recommends is to start the test JVM with Mockito's jar as an agent. Then nothing is attached dynamically, the warning disappears, and the suite will keep working when the default changes. In Maven, we let the dependency plugin expose the jar's path as a property and pass it to Surefire through argLine.
In Gradle the idea is the same: put mockito-core in a dedicated configuration and add jvmArgs("-javaagent:" + thatFile) to the test task. Notice the @{argLine} detail in Maven. Surefire's argLine is a single string, so if we overwrite it, a coverage agent added by another plugin quietly disappears. Placing this in a shared parent POM or build convention means every module gets it once, instead of each team copying it.
5The Limits That Remain
Even with the agent in place, retransformation has limits. Some core JDK types cannot be mocked because Mockito itself, or the JVM, depends on them: Mockito refuses to mock String, the primitive wrapper classes and Class, and it blocks static mocking of a few classes such as java.lang.Thread because changing them would break class loading or Mockito's own machinery. Native methods have no bytecode body to instrument, and some JDK internals live in modules whose classes cannot be retransformed safely. The exact list moves between versions, so check your version when a mock of a JDK class fails.
There is also a cost. Every instrumented class carries the dispatch check for the rest of the JVM's life, and retransforming many classes slows test startup. That is one more reason to use inline mocking deliberately, mostly at the edges of legacy code, rather than as the default way to isolate everything.
| Target | Subclass mock maker | Inline mock maker |
|---|---|---|
| Interface or open class | yes | yes |
| Final class or final method | no | yes |
| Static method | no | yes, thread-local |
Constructor (mockConstruction) | no | yes |
| String, wrappers, Class | no | no |
6Follow-up questions
Can I still use the subclass mock maker in Mockito 5?
Yes. Mockito ships it as mockito-subclass, or it can be selected through the mock maker configuration file. It avoids instrumentation entirely, which some teams prefer, but then finals and statics cannot be mocked.
Does -XX:+EnableDynamicAgentLoading solve the problem?
It hides the warning and keeps dynamic loading allowed, but it is a switch to restore old behaviour, not the recommended direction. Declaring the agent with -javaagent is the long-term answer because it matches what the JDK intends.
Why is the mock of a final class an instance of the real class?
Because the inline maker does not generate a subclass. It allocates an instance of the real class without running its constructor and registers it as a mock, and the instrumented method bodies consult that registration.
7Conclusion
In this answer, we saw that subclass-based mocking cannot reach final classes or static methods, so Mockito's inline mock maker uses a Byte Buddy agent to retransform real classes, inserting a check at the start of each method that hands control to Mockito for registered mocks. JDK 21 started warning about dynamically loaded agents and plans to disallow them by default, so we should start the test JVM with -javaagent pointing at mockito-core, for example through Surefire's argLine. Some JDK types still cannot be mocked.
Open with
The old subclass mock maker generates a subclass with Byte Buddy and overrides every method. That cannot work for a final class, a final method or a static method, because none of them can be overridden.
Then, if they want more
The inline mock maker, the default since Mockito 5, changes the real class instead. It attaches a Java agent through Byte Buddy and uses the instrumentation API to retransform the class.
Retransformation can change method bodies but cannot add fields, so Mockito inserts a check at the start of each method that asks whether this instance, or for statics this class on this thread, is a registered mock, and if so lets Mockito's handler answer.
What changed recently is that JDK 21, through JEP 451, warns when an agent is loaded dynamically into a running JVM and plans to disallow it by default.
Mockito's agent is loaded exactly that way, so the recommendation is to start the test JVM with -javaagent pointing at mockito-core, for example in Surefire's argLine, keeping the existing argLine so coverage agents survive. Some types still cannot be mocked, such as String, wrappers and Class, and a few JDK classes are blocked for static mocking.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Java 21
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- JUnit user guide, Mockito documentation
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "How does Mockito mock final classes and static methods, and what changed with recent...": https://scrivio-interview-library.netlify.app/java/mocking/25883fc0e2/