Mockito is a Java mocking framework that creates test doubles for a class's dependencies, letting you test a unit of code in isolation from the real implementations it collaborates with.
Key Points: • mock() creates a fake implementation of an interface or class with configurable behavior. • when()/thenReturn() (or doReturn()/when()) define what a mock should do when called. • verify() checks that expected interactions with a mock actually happened. • Mocking removes dependencies on slow or unpredictable resources like databases, network calls, or the file system. • It integrates with JUnit through annotations like @Mock, @InjectMocks, and the MockitoExtension.
Example: Instead of hitting a real payment API in a unit test, you mock the PaymentClient so charge() always returns a controlled success or failure response.
Interview Tip: A concise interview answer is:
"Mockito is a mocking framework that lets me replace a class's real dependencies with configurable fake objects, so I can test business logic in isolation without touching databases, networks, or other slow external systems. It's central to writing fast, deterministic unit tests."