Mockito.mock() Manual Creation | Java Fresher Interview
Fast recall
Quick revision
Core answer
Interview-ready guide
Interview answer
class).
That gives you a mock object right there in your code that works exactly like one created with the @Mock annotation. You'd use this style when annotations don't fit your situation.
For example, in a parameterized test, you might need a different mock for each parameter value—you can't do that with @Mock because that creates one mock for the entire test class. mock lets you preserve that type information properly. Another scenario is when you're writing a helper method that builds test data—maybe you have a factory method that creates a fully configured order with mocked dependencies.
mock there. The main trade-off is verbosity—manual mock creation is a bit wordier than just slapping @Mock on a field, but it gives you total control over when and how the mock is created. mock is there when you need the flexibility.
Mockito.mock() Manual Creation | Java Fresher Interview
Direct Answer
Use Mockito.mock(ClassName.class) to create a mock programmatically without annotations. This gives you the same mock object as @Mock but inline. Use it when you need to create mocks dynamically, work with generic types, or generate mocks in helper methods outside the test class lifecycle.
Code Example
class NotificationServiceTest {
@Test
void manualMockInTestMethod() {
// Create mock inline when you need it
EmailService emailService = Mockito.mock(EmailService.class);
when(emailService.send(anyString())).thenReturn(true);
var notificationService = new NotificationService(emailService);
assertTrue(notificationService.notifyUser("user@example.com"));
}
@ParameterizedTest
@ValueSource(strings = {"EmailService", "SmsService", "PushService"})
void dynamicMockCreationInParameterizedTest(String serviceType) {
// Create different mocks based on parameter
Object service = switch(serviceType) {
case "EmailService" -> Mockito.mock(EmailService.class);
case "SmsService" -> Mockito.mock(SmsService.class);
default -> Mockito.mock(PushNotificationService.class);
};
// Use the dynamically created mock in test logic
}
}
Common Mistakes & Pitfalls
- Not storing the mock reference: Calling Mockito.mock(Service.class) but not assigning it to a variable, so you can't stub or verify it later—the mock is created and immediately garbage collected.
- Mixing annotation and manual styles unnecessarily: Using both @Mock at the class level and Mockito.mock() in @BeforeEach for the same dependency, creating two different mock instances and causing confusion about which one is actually injected.
- Creating mocks in wrong scope: Making a mock in one test method and trying to use it in another—each test method is isolated and doesn't share local variables, so the mock is null in the second test.
Check your understanding
Answer these without looking back at the article.
- How do you mock static methods in Mockito?
- What is Mockito.reset() and when should you use it?
- How do you mock a class with a private constructor?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.