Quick revision
The short answer
EasyMock uses record, replay, verify: we record the expected calls on the mock, call replay(mock), run the code, then call verify(mock), and unexpected calls fail by default. Mockito uses stub-then-verify: mocks are lenient by default, we stub only what we need and verify the interactions we care about afterwards, which gives less brittle tests. JMockit can mock almost anything through a Java agent but is little maintained and treated as legacy, MockK is the idiomatic choice in Kotlin, and Spock has mocking built into Groovy specifications. Most Java teams use Mockito because of its readable API, Spring Boot integration, inline mocking of finals and statics, and active maintenance.
What to remember
In this answer, we compared the models behind the main mocking libraries. EasyMock records expected calls, replays them and verifies them, failing on unexpected calls by default. Mockito stubs only what is needed, lets other calls return defaults and verifies chosen interactions, which makes tests less brittle. JMockit mocks almost anything with an agent but is little maintained, MockK fits Kotlin and Spock fits Groovy. Mockito dominates Java because of its readable API, Spring Boot integration, inline mocking of finals and statics, and active maintenance.
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.
EasyMock uses record, replay, verify: we record the expected calls on the mock, call replay(mock), run the code, then call verify(mock), and unexpected calls fail by default. Mockito uses stub-then-verify: mocks are lenient by default, we stub only what we need and verify the interactions we care about afterwards, which gives less brittle tests. JMockit can mock almost anything through a Java agent but is little maintained and treated as legacy, MockK is the idiomatic choice in Kotlin, and Spock has mocking built into Groovy specifications. Most Java teams use Mockito because of its readable API, Spring Boot integration, inline mocking of finals and statics, and active maintenance.
Why this question is asked
Most of us learned Mockito first and never looked elsewhere, but legacy projects are full of EasyMock and JMockit tests, and Kotlin or Groovy modules bring their own tools. Interviewers ask this comparison to see whether we understand the different models behind the libraries, because the model decides how brittle the tests become. In this answer, we will write the same test in EasyMock's record-replay-verify style and in Mockito's stub-then-verify style, understand why the second is less brittle, look briefly at JMockit, MockK and Spock, and end with why Mockito became the default.
1A Quick Recap: What a Mocking Library Does
A mock is a stand-in for a collaborator. Every mocking library lets us do three things: create the stand-in, define what it returns, and check how the code under test used it. The libraries differ mainly in when we describe the expected calls and what happens when the code makes a call we did not describe. Those two choices shape every test written with them.
We will use the same small example for each: an OrderService that loads an order from an OrderRepository and sends a confirmation through a Mailer.
2EasyMock: Record, Replay, Verify
EasyMock works in three phases. In the record phase, the mock is like a tape recorder: every call we make on it is recorded as an expectation, and expect(...).andReturn(...) says what it should return. Then replay(mock) switches it to the replay phase, where it plays back the recorded behaviour while the real code runs. Finally verify(mock) checks that every expected call actually happened.
Notice the consequence of recording every expected call up front. A default EasyMock mock fails on any call that was not recorded, with an error like 'Unexpected method call'. createStrictMock() also checks the order of calls, and createNiceMock() is the lenient variant that returns defaults for unexpected calls. So with the default and strict styles, if confirm() later also calls repo.markConfirmed(7L), the test fails, even though the behaviour we cared about is unchanged.
3Mockito: Stub, Then Verify
Mockito removed the phases. A mock is always ready: calls we did not stub return default values (null, 0, false, empty collections), and nothing fails just because a call was made. We stub only the calls whose return values the code needs, run the code, and then verify only the interactions that matter.
7 messages in order. Play them, or step through them one at a time.
EasyMock describes every call up front; Mockito describes only what the test needs.
The reason this is less brittle is that the test describes only what it needs and what it checks. Extra calls are allowed unless we explicitly forbid them. Mockito's strict stubs mode, the default with MockitoExtension, adds safety in the other direction by failing on stubs that were never used, which catches stale setup without punishing new calls.
4JMockit, MockK and Spock
JMockit took a different route: it runs as a Java agent, a component that rewrites class bytecode, so it can mock almost anything, including constructors, static methods, final classes and private methods, using Expectations and Verifications blocks. That power came at a price. It needs the agent on the test JVM, it depends on JVM internals that change with new JDKs, and it has seen little maintenance in recent years, so projects report breakage on newer Java versions. It is best treated as legacy and migrated away from.
MockK is the idiomatic library for Kotlin. Kotlin classes are final by default and Kotlin has coroutines, extension functions and objects, and MockK supports these naturally with a Kotlin DSL such as every { repo.find(7) } returns order and coVerify for suspend functions. In a Kotlin code base it is usually a better fit than Mockito.
Spock is a testing framework for Groovy that includes mocking in its specification language. Interactions are written as 1 * mailer.send("a@b.com") in a then: block, which reads well and gives good failure messages. It suits teams that already write tests in Groovy, but it adds a second language to a Java project.
| Library | Model | Status today |
|---|---|---|
EasyMock | record, replay, verify; strict by default | maintained, mostly in legacy code |
| Mockito | stub, then verify; lenient calls, strict stubs | default for Java, very active |
| JMockit | agent-based, mocks almost anything | little maintained, treat as legacy |
| MockK | Kotlin DSL, coroutines, finals | idiomatic for Kotlin |
| Spock | interactions in Groovy specs | good fit for Groovy test suites |
5Why Most Java Teams Use Mockito
Several reasons add up. Its API reads like a sentence: when this, then return that; verify this. Its default model produces tests that survive refactoring better than strict record-replay. Spring Boot's test starter includes it, and Spring's @MockitoBean integrates it with the application context, so it is already on the classpath of almost every Spring project. Since Mockito 5, the inline mock maker is the default, so final classes and static methods can be mocked without extra libraries like PowerMock. And it is actively maintained, with releases that follow new JDK versions quickly, which matters more every year as the JDK changes faster.
None of this makes the other libraries wrong. It means that for a new Java project, Mockito is the choice with the least friction and the most shared knowledge, while MockK and Spock make sense in Kotlin and Groovy code bases.
6Follow-up questions
Can EasyMock be used in a Mockito-like way?
Partly. createNiceMock() returns defaults for unexpected calls, so tests only fail on missing expected calls. But the record and replay phases remain, so the structure of the test stays different.
Is PowerMock still needed?
No for new code. PowerMock was used to mock statics, finals and constructors, but it is not maintained for modern JDKs, and Mockito's inline mock maker covers those cases with mockStatic and mockConstruction.
Can Mockito and MockK be used in the same project?
Yes, in different modules or test classes. Kotlin tests can use Mockito with the mockito-kotlin helpers, but MockK handles Kotlin features such as suspend functions and objects more naturally.
7Conclusion
In this answer, we compared the models behind the main mocking libraries. EasyMock records expected calls, replays them and verifies them, failing on unexpected calls by default. Mockito stubs only what is needed, lets other calls return defaults and verifies chosen interactions, which makes tests less brittle. JMockit mocks almost anything with an agent but is little maintained, MockK fits Kotlin and Spock fits Groovy. Mockito dominates Java because of its readable API, Spring Boot integration, inline mocking of finals and statics, and active maintenance.
Open with
The libraries differ mostly in their model. EasyMock is record, replay, verify: I call the methods I expect on the mock to record them, with expect and andReturn for values, then call replay, run the code, and finally verify that every expected call happened.
Then, if they want more
A default EasyMock mock fails on any call I did not record, and a strict mock also checks order, so tests break when the code makes a harmless extra call.
Mockito is stub then verify: mocks are lenient, unstubbed calls return defaults, I stub only what the code needs and verify only the interactions I care about, so tests survive refactoring better. With MockitoExtension, strict stubs still catch unused stubs.
JMockit uses a Java agent to mock almost anything, including constructors and statics, but it is little maintained and breaks on newer JDKs, so I treat it as legacy. MockK is the natural choice for Kotlin, with support for final classes and coroutines, and Spock gives mocking inside Groovy specifications.
Most Java teams use Mockito because the API is readable, it ships with Spring Boot's test starter and integrates through @MockitoBean, its inline mock maker handles finals and statics, and it is actively maintained.
About this answer
- Last updated
- 12 November 2026
- 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 "Compare Mockito with EasyMock, JMockit, MockK and Spock mocks. What is the...": https://scrivio-interview-library.netlify.app/java/mocking/2756b3ed95/