Mockito Interview Questions
Interviewing for a Java SDET or developer role that uses Mockito? Practice with an AI that asks about mocks vs spies, stubbing with when/thenReturn, verifying interactions, argument matchers, and the pitfalls that trip people up.
Free to start · 7-day trial on paid plans
What you’ll be asked.
Mockito interviews focus on the difference between a mock, a spy, and a stub, how you stub behavior with when/thenReturn (and doReturn for spies), and how you verify interactions with verify, times, never, and InOrder. Expect questions on argument matchers (any, eq, argThat) and the rule that you cannot mix raw values and matchers, on annotations (@Mock, @Spy, @InjectMocks) and the MockitoExtension, and on testing exceptions with thenThrow. Senior topics cover mocking static and final methods (mockito-inline), avoiding over-mocking, and why mocking what you do not own is a smell.
Topics covered.
Key areas interviewers evaluate when asking about mockito.
Mocks, Spies & Stubs
The difference between a mock, a partial spy, and a stub, and when each is the right tool.
Stubbing Behaviour
when/thenReturn, thenThrow, doReturn for spies, consecutive returns, and stubbing voids.
Verifying Interactions
verify, times, never, atLeast, InOrder, and verifying no more interactions.
Argument Matchers
any, eq, argThat, the all-or-nothing matcher rule, and capturing arguments with ArgumentCaptor.
Annotations & Setup
@Mock, @Spy, @InjectMocks, MockitoExtension/JUnit integration, and avoiding stubbing leaks.
Pitfalls & Best Practice
Over-mocking, mocking types you do not own, mockito-inline for static/final, and keeping tests readable.
Sample Interview Questions
Questions based on real interview patterns. Practice answering these with AssertHired’s AI interviewer.
- 01
What is the difference between a mock and a spy in Mockito? When would you use each?
- 02
How do you stub a method to return a value, and how is stubbing a spy different?
- 03
How do you verify that a method was called exactly twice, and never with a particular argument?
- 04
What is the rule about mixing argument matchers and raw values, and why does it exist?
- 05
How do @Mock, @Spy, and @InjectMocks work, and what does @InjectMocks actually do?
- 06
How would you test that a method throws an exception using Mockito?
- 07
How do you mock a static or final method, and why was that historically discouraged?
- 08
When is mocking a code smell? How do you avoid over-mocking?
How AssertHired works.
Three steps. No fluff. Designed specifically for QA engineers.
Pick Your Focus
Choose from 6 QA-specific categories. Select your role, target company, and difficulty level to customize the experience.
Interview with AI
Answer 5 realistic interview questions from an AI that understands QA workflows, test architecture, and engineering culture.
Get Scored
Receive instant feedback scored across 4 dimensions: Technical Accuracy, Communication, Examples, and Depth of Knowledge.
Frequently Asked Questions
What is the difference between a mock and a spy?
A mock is a full test double with no real behavior unless you stub it. A spy wraps a real object and runs real methods unless you override specific ones. Use mocks to isolate completely; use spies sparingly when you need mostly-real behavior with a couple of overrides.
Why can you not mix matchers and raw values in Mockito?
Mockito uses matchers via a side-channel stack, so if one argument is a matcher, all must be. Mixing a raw value with a matcher throws InvalidUseOfMatchersException. Wrap raw values with eq() to fix it.
Is it bad practice to mock everything?
Yes. Over-mocking couples tests to implementation and hides real integration bugs. Prefer mocking at boundaries (network, database, time), use real objects for simple collaborators, and never mock types you do not own, wrap them instead.
Can I practice Mockito questions on AssertHired?
Yes. The AI interviewer asks Mockito mocking, verification, and best-practice questions with follow-ups and scores you across four dimensions.
From the Blog
Related guides and tips to help you prepare.
Explore More Interview Prep Resources
Dive deeper into related QA interview topics.
Free QA career tools, no account needed
Instant and private, everything runs in your browser. Try them before you sign up.
QA Resume Checker
Instant 0-100 score on automation keywords, impact, and ATS formatting.
QA Cover Letter Generator
A tailored 3-paragraph QA cover letter from your resume and a job post.
QA Application Tracker
Drag-and-drop kanban to track every QA application from Applied to Offer.
QA Take-Home Test Generator
A realistic take-home assignment with a scenario, tasks, and a rubric.
QA LinkedIn Headline Generator
A recruiter-searchable headline, About section, and skills list.
QA STAR Story Builder
Structure a QA behavioral answer with the STAR method and instant checks.
QA Bug Report Generator
Build a clean, reproducible bug report for Markdown, Jira, or plain text.
Boundary Value Analysis Generator
Generate boundary value and equivalence partitioning test cases from a range.
QA Metrics Calculator
Calculate DRE, defect leakage, defect density, and pass rate with interpretation.
QA Test Plan Generator
Build a structured test plan (scope, approach, criteria, risks) in Markdown.
Ready for Your Mockito Interview?
Practice with AI that asks real Mockito mocking, verification, and Java testing questions.
Join 1,200+ QA engineers already practicing with AssertHired.
Start your free QA interview