LearnThatStack Ace your next interview
Topic · part of Testing
Mockito.
29 Qs 4 free
Change topic Change
Drill · questions

All questions

of 29
Beginner 7
01

What is Mockito and why is it used in unit testing?

Beginner ·

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:

Keep going - a few more words and AI can grade it.

Last attempt -

Your answer

Re-explain

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.

Rewriting in plainer words…

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

Tailored explanation · switch back to · ·
Point the redraw:
How well did you know this?
AI:

02

How do you add Mockito to a Java project?

Beginner ·

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:

Keep going - a few more words and AI can grade it.

Last attempt -

Your answer

Re-explain

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>
Rewriting in plainer words…

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

Tailored explanation · switch back to · ·
Point the redraw:
How well did you know this?
AI:

03

What are the different ways to create a mock in Mockito?

Beginner ·

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:

Keep going - a few more words and AI can grade it.

Last attempt -

Your answer

Re-explain

There are several ways to create mocks in Mockito:

  1. Using mock() method:
List<String> mockList = mock(List.class);
  1. Using @Mock annotation:
@Mock
private List<String> mockList;
  1. Using @MockBean (Spring Boot):
@MockBean
private UserService userService;
  1. Using MockitoAnnotations.openMocks():
@BeforeEach
void setUp() {
    MockitoAnnotations.openMocks(this);
}

The annotation approach is preferred as it makes tests cleaner and more readable.

Rewriting in plainer words…

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

Tailored explanation · switch back to · ·
Point the redraw:
How well did you know this?
AI:

04

How do you verify that a method was called in Mockito?

Beginner ·

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:

Keep going - a few more words and AI can grade it.

Last attempt -

Your answer

Re-explain

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.

Rewriting in plainer words…

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

Tailored explanation · switch back to · ·
Point the redraw:
How well did you know this?
AI:

05

What are argument matchers and when should you use them?

Part of Pro
06

How do you mock methods to throw exceptions?

Part of Pro
07

How do you mock void methods?

Part of Pro
Intermediate 10
08

What is the difference between a mock, stub, and spy?

Part of Pro
09

Explain the difference between when().thenReturn() and doReturn().when()

Part of Pro
10

How do you stub consecutive calls to return different values?

Part of Pro
11

What is the difference between verify() and verifyNoInteractions()?

Part of Pro
12

How do you verify the order of method calls?

Part of Pro
13

What is the @Captor annotation and how is it used?

Part of Pro
14

When should you use @Spy instead of @Mock?

Part of Pro
15

Explain the difference between @Mock, @MockBean, and @InjectMocks

Part of Pro
16

How do you test that your code properly handles exceptions?

Part of Pro
17

What are some Mockito best practices and common pitfalls?

Part of Pro
Expert 12
18

How do you create custom argument matchers?

Part of Pro
19

What are the limitations of spying on objects?

Part of Pro
20

How does @InjectMocks work and what are its limitations?

Part of Pro
21

How do you mock static methods in Mockito?

Part of Pro
22

How do you mock final classes and methods?

Part of Pro
23

What is MockedConstruction and when would you use it?

Part of Pro
24

What is the Answer interface and how is it used?

Part of Pro
25

How do you handle flaky tests caused by Mockito?

Part of Pro
26

What are the performance implications of using Mockito?

Part of Pro
27

How do you test interaction between multiple mocked objects?

Part of Pro
28

What are MockMaker and Mockito extensions?

Part of Pro
29

How do you handle Mockito with modern Java features?

Part of Pro

No matches

Try a different filter or search term.

Pro · $10/mo

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.

Technologies
No technologies match “”.
Cross-cutting topics
No topics match “”.
By role
Stacks & frameworks

MEAN

MongoDB, Express, Angular, Node.js

MERN

MongoDB, Express, React, Node.js

LAMP

Linux, Apache, MySQL, PHP

Django

Python Full-Stack Development

Ruby on Rails

Convention over Configuration

JAM

JavaScript, APIs, and Markup

Serverless on AWS

Serverless Architecture on AWS

Cross-cutting topics 43 topics

Interviewers also test these - they're common to every stack, whichever one you picked above.

Flutter Mobile

Flutter Cross-Platform Mobile Development

Cross-cutting topics 44 topics

Interviewers also test these - they're common to every stack, whichever one you picked above.

Spring Boot

Enterprise Java Development

.NET

Microsoft Ecosystem

Vue

Vue.js, Vite, TypeScript, Tailwind, Node.js

Go Backend

Golang, gRPC, PostgreSQL, Redis, RabbitMQ

FastAPI

Python, FastAPI, SQLAlchemy, PostgreSQL

React Native

React, TypeScript, Redux, Firebase

iOS Native

Swift, SwiftUI, UIKit, Firebase

Android Native

Java, Jetpack Compose, Firebase

Web3 / Ethereum

Solidity, Ethereum, Hardhat, Foundry

DevOps / Platform

Docker, Kubernetes, Terraform, CI/CD

Core SWE Interview Prep

Data structures, algorithms, OS, concurrency, networking, git
Big-O & Complexity Analysis Arrays, Strings & Hash Tables Linked Lists, Stacks & Queues Trees, BSTs & Heaps Graphs Sorting, Searching & Recursion Operating Systems Concurrency & Multithreading Networking for Developers Git & Version Control API Design 45 Distributed Systems Fundamentals 34

Cross-cutting topics 43 topics

Interviewers also test these - they're common to every stack, whichever one you picked above.


Cross-cutting topics 45 topics

Interviewers also test these - they're common to every stack, whichever one you picked above.