13 September 2025

#Mockito

#Mockito

Key Concepts


Topic Sub-Topics (comma separated) Basic Intermediate Advanced Expert
Mockito Basics Introduction, What is Mockito, Benefits, Setup with Maven/Gradle, Simple Example
Annotations @Mock, @InjectMocks, @Spy, @Captor, @RunWith(MockitoJUnitRunner.class), @ExtendWith(MockitoExtension.class)
Mocking & Stubbing Creating mocks, when()?thenReturn(), thenThrow(), doReturn(), doThrow(), void methods, Chained stubbing
Verification verify(), times(), never(), atLeast(), atMost(), inOrder(), timeout(), verifyNoMoreInteractions()
Argument Matchers any(), eq(), ArgumentCaptor, Custom matchers, Hamcrest matchers
Spies @Spy annotation, Difference between mock() and spy(), Partial mocking
Mocking Advanced Cases Static methods (Mockito.mockStatic), Final classes, Private methods (with PowerMockito), Constructors
BDD Style BDDMockito.given(), willReturn(), willThrow(), then(), BDD vs Classic Mockito
Integration with JUnit JUnit 4 Runner, JUnit 5 Extension, Assertions with Mockito, Parameterized tests
Spring Integration @MockBean, @SpyBean, SpringBootTest with Mockito, Injecting mocks into Spring context
Exception Testing thenThrow(), doThrow(), Custom exceptions, Checked vs Unchecked exception mocking
InOrder Verification Multiple mocks order verification, Complex interaction order checks, inOrder with argument matchers
Timeout & Async Testing verify(mock, timeout()), Async behavior verification, Advanced concurrency scenarios
Best Practices Test readability, Avoiding overuse of mocks, Test maintenance, Test isolation principles
Limitations & Pitfalls What Mockito cannot do, Common mistakes, Alternatives (EasyMock, JMockit, PowerMockito), Migration strategies

Interview question

1. Mockito Basics (20 Questions)

  1. What is Mockito, and why is it used in unit testing?
  2. How does Mockito differ from JUnit?
  3. How do you create a simple mock object using Mockito?
  4. Explain the benefits of using Mockito in test-driven development.
  5. How do you add Mockito dependencies in Maven or Gradle?
  6. What are the main features of Mockito?
  7. Can Mockito mock interfaces and abstract classes?
  8. How do you annotate a test class to use Mockito?
  9. What is the role of the MockitoAnnotations.initMocks() method?
  10. Difference between mock() method and @Mock annotation?
  11. Can Mockito be used without JUnit?
  12. What is the default behavior of a mock object in Mockito?
  13. What happens if you call a non-stubbed method on a mock?
  14. How does Mockito improve test readability?
  15. Why is mocking preferred over stubbing real objects?
  16. What is the default return value of primitive types in Mockito?
  17. Can Mockito mock generic types?
  18. What is the difference between a real object and a mock object?
  19. Why is Mockito considered lightweight compared to PowerMockito?
  20. Explain limitations of Mockito compared to real implementations.

2. Annotations (20 Questions)

  1. What is the purpose of the @Mock annotation?
  2. How is @InjectMocks different from @Mock?
  3. Explain the use of @Spy in Mockito.
  4. How does @Captor work in Mockito?
  5. What is the role of @RunWith(MockitoJUnitRunner.class)?
  6. How does JUnit 5 handle Mockito annotations?
  7. Explain the use of @ExtendWith(MockitoExtension.class).
  8. What is the difference between @Spy and @Mock?
  9. How does @InjectMocks help in dependency injection?
  10. Can you use @MockBean in Spring Boot tests?
  11. What are the risks of overusing @InjectMocks?
  12. How do you initialize mocks without using runners/extensions?
  13. What is the difference between @MockBean and @Mock in Spring?
  14. How do you use multiple @Captor annotations in a test?
  15. Can annotations be used together in the same test class?
  16. How do you mock a dependency injected via constructor using @InjectMocks?
  17. What happens if you forget to initialize @Mock annotations?
  18. How do you combine Mockito with JUnit5 Jupiter annotations?
  19. What is the advantage of using annotations over manual mock creation?
  20. Can you combine Mockito annotations with Spring Boot test annotations?

3. Mocking & Stubbing (20 Questions)

  1. How do you stub a method to return a value in Mockito?
  2. What is the difference between when().thenReturn() and doReturn().when()?
  3. How do you stub a void method in Mockito?
  4. Can you stub consecutive calls with different return values?
  5. How do you handle exceptions in stubbing?
  6. Explain chained stubbing in Mockito.
  7. What happens if you stub the same method multiple times?
  8. How do you stub methods that return collections or arrays?
  9. Difference between stubbing real methods vs mock methods?
  10. How do you mock final methods in Mockito?
  11. What happens when you don?t stub a method in Mockito?
  12. How to mock methods that return Optional in Java 8?
  13. How do you mock default methods in interfaces?
  14. Can you stub private methods directly in Mockito?
  15. How do you simulate network failure with stubbing?
  16. How to simulate delay or timeout in stubbing?
  17. Can you stub methods of an abstract class?
  18. How do you stub methods to return null values?
  19. Difference between doThrow() and thenThrow()?
  20. What are best practices for stubbing in Mockito?

4. Verification (20 Questions)

  1. What is the purpose of the verify() method in Mockito?
  2. How do you verify that a method was called once?
  3. How to verify that a method was never called?
  4. How to verify the number of method invocations?
  5. Explain atLeast() and atMost() in verification.
  6. How do you verify the order of method calls?
  7. What happens if a method is called more times than verified?
  8. Can you verify overloaded methods?
  9. How do you verify no more interactions occurred?
  10. Difference between verify() and verifyNoInteractions()?
  11. How to verify that a method was called with specific arguments?
  12. How do you use ArgumentCaptor in verification?
  13. Can you verify async calls with Mockito?
  14. How does timeout() work in verification?
  15. How do you verify multiple mocks together?
  16. Can you verify methods on a spy object?
  17. Difference between inOrder() verification and multiple verify()?
  18. How do you verify methods in a loop?
  19. What happens if you verify a non-stubbed method?
  20. Best practices for verification in unit tests?

5. Argument Matchers (20 Questions)

  1. What are argument matchers in Mockito?
  2. Difference between eq() and any()?
  3. Can you use custom argument matchers?
  4. How to implement ArgumentMatcher interface?
  5. What happens if you mix argument matchers and raw values?
  6. How does ArgumentCaptor work in detail?
  7. How to capture multiple values with ArgumentCaptor?
  8. How do you verify arguments of overloaded methods?
  9. Difference between assertEquals() and using matchers?
  10. Can you use Hamcrest matchers in Mockito?
  11. Can ArgumentCaptor capture generic types?
  12. How to use multiple matchers in the same method?
  13. What happens if no argument matches?
  14. Can you capture arguments from multiple calls?
  15. How to use matchers with varargs methods?
  16. Difference between ArgumentCaptor.capture() vs getValue()?
  17. How do you chain multiple argument matchers?
  18. Can matchers be used with spies?
  19. What is the difference between captor and verify()?
  20. Best practices for argument matchers?

6. Spies (20 Questions)

  1. What is a spy in Mockito?
  2. Difference between a mock and a spy?
  3. How do you create a spy object?
  4. What is partial mocking in Mockito?
  5. When should you use a spy over a mock?
  6. How to stub methods in a spy?
  7. Can you spy on final classes?
  8. What happens if you don?t stub a spy method?
  9. Can you spy on private methods?
  10. How do you spy on collections like List/Map?
  11. How to verify method calls on a spy object?
  12. Difference between real method calls and spy behavior?
  13. Can you use spies with @Spy annotation?
  14. Can spies throw exceptions like mocks?
  15. How do spies handle default return values?
  16. What are common pitfalls with spies?
  17. Can you combine spies and mocks in the same test?
  18. How does a spy behave in dependency injection?
  19. Can spies be used with @InjectMocks?
  20. Best practices for using spies in unit testing?

7. Advanced Mocking (20 Questions)

  1. How do you mock static methods in Mockito?
  2. Can you mock constructors in Mockito?
  3. How do you mock private methods with PowerMockito?
  4. Can you mock final classes without PowerMockito?
  5. What is deep stubbing in Mockito?
  6. How do you enable deep stubbing?
  7. When should deep stubbing be avoided?
  8. How to mock chained method calls?
  9. What is the drawback of deep stubs?
  10. How do you mock multiple return values for chained calls?
  11. Can you mock methods with generics?
  12. How to mock utility classes with static methods?
  13. How to mock singleton classes?
  14. Can you mock default methods in interfaces?
  15. Difference between mockStatic() and mockConstruction()?
  16. What is the role of MockedStatic in Mockito?
  17. How to verify static method calls?
  18. How to reset static mocks after test execution?
  19. Can you mock enum methods?
  20. Limitations of advanced mocking in Mockito?

8. BDD Style (20 Questions)

  1. What is BDDMockito in Mockito?
  2. Difference between BDDMockito.given() and when()?
  3. How to use willReturn() in BDD style?
  4. How to use willThrow() in BDD style?
  5. Difference between then() and verify()?
  6. How to structure BDD-style test cases?
  7. Benefits of using BDD style in Mockito?
  8. Can BDDMockito work with spies?
  9. How to verify multiple interactions in BDD style?
  10. Can you mix BDDMockito with classic Mockito syntax?
  11. Example of given-when-then using Mockito?
  12. What is the difference between assertThat() in BDD and JUnit assertions?
  13. How do you improve readability using BDDMockito?
  14. Can BDDMockito be integrated with Cucumber?
  15. How to chain multiple BDD stubbings?
  16. Can you use ArgumentCaptor in BDD style?
  17. How do you test exceptions in BDD style?
  18. Is BDDMockito limited to JUnit only?
  19. What are best practices for BDD-style tests?
  20. When not to use BDDMockito?

9. Integration with JUnit & Spring (20 Questions)

  1. How do you integrate Mockito with JUnit 4?
  2. How do you integrate Mockito with JUnit 5?
  3. What is the purpose of MockitoJUnitRunner?
  4. How to use MockitoExtension in JUnit 5?
  5. Can you use Mockito with TestNG?
  6. Difference between @MockBean and @Mock in Spring Boot tests?
  7. How do you test REST controllers with Mockito?
  8. How do you test service layer with @InjectMocks?
  9. Can Mockito mock Spring Data JPA repositories?
  10. What is @SpyBean in Spring Boot?
  11. How do you use Mockito with @SpringBootTest?
  12. Difference between Mockito and MockMvc?
  13. How do you mock external API calls in Spring Boot with Mockito?
  14. Can Mockito work with dependency injection frameworks other than Spring?
  15. How do you inject mocks in beans with constructors?
  16. How do you test @Transactional methods with Mockito?
  17. How to mock Spring Security dependencies with Mockito?
  18. Can you mock environment variables in Spring tests?
  19. How to use Mockito with Spring Boot test slices?
  20. Best practices for integrating Mockito with Spring?

10. Best Practices & Pitfalls (20 Questions)

  1. What are common pitfalls in using Mockito?
  2. Why should you avoid overusing mocks?
  3. What is a test smell in mocking?
  4. How do you prevent flaky tests with Mockito?
  5. Why should you prefer constructor injection with @InjectMocks?
  6. How to avoid NullPointerExceptions with mocks?
  7. When to use mocks vs real objects?
  8. What is the danger of verifying implementation details?
  9. How to structure tests for readability with Mockito?
  10. Why is it important to reset mocks between tests?
  11. What is the role of Mockito.reset()?
  12. Difference between using mocks in unit vs integration testing?
  13. Why should you avoid deep stubbing?
  14. When should you not use spies?
  15. How to ensure isolation in unit tests with mocks?
  16. Why avoid mocking value objects (e.g., String, DTOs)?
  17. How to ensure maintainability of Mockito-based tests?
  18. What is the relationship between mocking and TDD?
  19. How does Mockito help achieve loose coupling in tests?
  20. Final best practices for enterprise-grade testing with Mockito?


Related Topics