Interview Questions

Get ready for your next interview with our comprehensive question library

Mockito Interview Questions

Filter by Difficulty

1.

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

beginner

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.

2.

How do you add Mockito to a Java project?

beginner

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>
3.

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

beginner

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.

4.

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

beginner

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.

5.

What are argument matchers and when should you use them?

beginner

Upgrade to Premium to see the answer

Upgrade to Premium
6.

How do you mock methods to throw exceptions?

beginner

Upgrade to Premium to see the answer

Upgrade to Premium
7.

How do you mock void methods?

beginner

Upgrade to Premium to see the answer

Upgrade to Premium
8.

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

intermediate

Upgrade to Premium to see the answer

Upgrade to Premium
9.

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

intermediate

Upgrade to Premium to see the answer

Upgrade to Premium
10.

How do you stub consecutive calls to return different values?

intermediate

Upgrade to Premium to see the answer

Upgrade to Premium
11.

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

intermediate

Upgrade to Premium to see the answer

Upgrade to Premium
12.

How do you verify the order of method calls?

intermediate

Upgrade to Premium to see the answer

Upgrade to Premium
13.

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

intermediate

Upgrade to Premium to see the answer

Upgrade to Premium
14.

When should you use @Spy instead of @Mock?

intermediate

Upgrade to Premium to see the answer

Upgrade to Premium
15.

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

intermediate

Upgrade to Premium to see the answer

Upgrade to Premium
16.

How do you test that your code properly handles exceptions?

intermediate

Upgrade to Premium to see the answer

Upgrade to Premium
17.

What are some Mockito best practices and common pitfalls?

intermediate

Upgrade to Premium to see the answer

Upgrade to Premium
18.

How do you create custom argument matchers?

expert

Upgrade to Premium to see the answer

Upgrade to Premium
19.

What are the limitations of spying on objects?

expert

Upgrade to Premium to see the answer

Upgrade to Premium
20.

How does @InjectMocks work and what are its limitations?

expert

Upgrade to Premium to see the answer

Upgrade to Premium
Showing 1 to 20 of 29 results

Premium Plan

$10.00 /monthly
  • Access all premium content - interview questions, and other learning resources

  • We regularly update our features and content, to ensure you get the most relevant and updated premium content.

  • 1000 monthly credits

  • Cancel anytime