All questions
of 29What is Mockito and why is it used in unit testing?
Answer it yourself first - out loud, or typed below.
How should your speech become text?
Listening… your words appear above as you speak - tap Stop when you're done.
Recording · cr - tap Stop & transcribe when you're done.
Transcribing with AI…
Voice:
Last attempt -
Mockito is a popular Java mocking framework used for unit testing. It allows developers to create mock objects that simulate the behavior of real objects in a controlled way. The main purposes of Mockito are:
- Isolation: Test classes in isolation by mocking their dependencies
- Control: Define specific behaviors for method calls during testing
- Verification: Verify that certain methods were called with expected parameters
- Speed: Avoid expensive operations like database calls or network requests
Mockito helps create fast, reliable, and focused unit tests by eliminating external dependencies.
This answer doesn't lend itself to a diagram - it reads best . No credits were charged.
The model's verdict: “”
The interactive diagram is below the answer - jump to diagram ↓
This answer is explained by a shared concept diagram - open →
How do you add Mockito to a Java project?
Answer it yourself first - out loud, or typed below.
How should your speech become text?
Listening… your words appear above as you speak - tap Stop when you're done.
Recording · cr - tap Stop & transcribe when you're done.
Transcribing with AI…
Voice:
Last attempt -
Maven:
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>5.7.0</version>
<scope>test</scope>
</dependency>
Gradle:
testImplementation 'org.mockito:mockito-core:5.7.0'
JUnit 5 integration:
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-junit-jupiter</artifactId>
<version>5.7.0</version>
<scope>test</scope>
</dependency>
This answer doesn't lend itself to a diagram - it reads best . No credits were charged.
The model's verdict: “”
The interactive diagram is below the answer - jump to diagram ↓
This answer is explained by a shared concept diagram - open →
What are the different ways to create a mock in Mockito?
Answer it yourself first - out loud, or typed below.
How should your speech become text?
Listening… your words appear above as you speak - tap Stop when you're done.
Recording · cr - tap Stop & transcribe when you're done.
Transcribing with AI…
Voice:
Last attempt -
There are several ways to create mocks in Mockito:
- Using mock() method:
List<String> mockList = mock(List.class);
- Using @Mock annotation:
@Mock
private List<String> mockList;
- Using @MockBean (Spring Boot):
@MockBean
private UserService userService;
- Using MockitoAnnotations.openMocks():
@BeforeEach
void setUp() {
MockitoAnnotations.openMocks(this);
}
The annotation approach is preferred as it makes tests cleaner and more readable.
This answer doesn't lend itself to a diagram - it reads best . No credits were charged.
The model's verdict: “”
The interactive diagram is below the answer - jump to diagram ↓
This answer is explained by a shared concept diagram - open →
How do you verify that a method was called in Mockito?
Answer it yourself first - out loud, or typed below.
How should your speech become text?
Listening… your words appear above as you speak - tap Stop when you're done.
Recording · cr - tap Stop & transcribe when you're done.
Transcribing with AI…
Voice:
Last attempt -
Mockito provides the verify() method to check if methods were called:
// Verify method was called once
verify(mockList).add("item");
// Verify method was called specific number of times
verify(mockList, times(2)).add(anyString());
// Verify method was never called
verify(mockList, never()).clear();
// Verify method was called at least/at most certain times
verify(mockList, atLeast(1)).size();
verify(mockList, atMost(3)).add(anyString());
Verification should be done after the method under test has been executed.
This answer doesn't lend itself to a diagram - it reads best . No credits were charged.
The model's verdict: “”
The interactive diagram is below the answer - jump to diagram ↓
This answer is explained by a shared concept diagram - open →
What are argument matchers and when should you use them?
How do you mock methods to throw exceptions?
How do you mock void methods?
What is the difference between a mock, stub, and spy?
Explain the difference between when().thenReturn() and doReturn().when()
How do you stub consecutive calls to return different values?
What is the difference between verify() and verifyNoInteractions()?
How do you verify the order of method calls?
What is the @Captor annotation and how is it used?
When should you use @Spy instead of @Mock?
Explain the difference between @Mock, @MockBean, and @InjectMocks
How do you test that your code properly handles exceptions?
What are some Mockito best practices and common pitfalls?
How do you create custom argument matchers?
What are the limitations of spying on objects?
How does @InjectMocks work and what are its limitations?
How do you mock static methods in Mockito?
How do you mock final classes and methods?
What is MockedConstruction and when would you use it?
What is the Answer interface and how is it used?
How do you handle flaky tests caused by Mockito?
What are the performance implications of using Mockito?
How do you test interaction between multiple mocked objects?
What are MockMaker and Mockito extensions?
How do you handle Mockito with modern Java features?
This answer is part of Pro.
The full written answer, with the trade-offs and follow-ups an interviewer will probe.
No matches
Try a different filter or search term.
25 of 29 Mockito answers are gated.
Full answers, code samples, AI explanations - simpler, deeper, or as an interactive diagram. Cancel anytime.
- Full answers + code
- AI explain - simpler, deeper, or visualized
- 1,000 AI credits / month
- Cancel anytime
Change topic
Pick a different technology or stack. Your current topic stays put until you choose a new one.
MEAN
MongoDB, Express, Angular, Node.jsMERN
MongoDB, Express, React, Node.jsLAMP
Linux, Apache, MySQL, PHPRuby on Rails
Convention over ConfigurationJAM
JavaScript, APIs, and MarkupServerless on AWS
Serverless Architecture on AWSInterviewers also test these - they're common to every stack, whichever one you picked above.
Flutter Mobile
Flutter Cross-Platform Mobile DevelopmentInterviewers also test these - they're common to every stack, whichever one you picked above.
Spring Boot
Enterprise Java Development.NET
Microsoft EcosystemVue
Vue.js, Vite, TypeScript, Tailwind, Node.jsGo Backend
Golang, gRPC, PostgreSQL, Redis, RabbitMQFastAPI
Python, FastAPI, SQLAlchemy, PostgreSQLReact Native
React, TypeScript, Redux, FirebaseiOS Native
Swift, SwiftUI, UIKit, FirebaseAndroid Native
Java, Jetpack Compose, FirebaseWeb3 / Ethereum
Solidity, Ethereum, Hardhat, FoundryDevOps / Platform
Docker, Kubernetes, Terraform, CI/CDCore SWE Interview Prep
Data structures, algorithms, OS, concurrency, networking, gitInterviewers also test these - they're common to every stack, whichever one you picked above.
Interviewers also test these - they're common to every stack, whichever one you picked above.