Mockito verify() Method | Java Fresher Interview
Fast recall
Quick revision
Core answer
Interview-ready guide
Interview answer
I'd say verify is how you check that your code called a method on a mock with the right arguments.
It's behavior verification — you're asserting that an interaction happened, not checking what it returned. So if I've got a service that calls orderRepository dot save, I write verify orderRepository dot save order to make sure that call actually occurred. This matters a lot for void methods or methods with side effects, like saving to a database or sending an email.
There's no return value to assert on, so verify is how you confirm the action took place. By default, verify checks the method was called exactly once. If it wasn't called at all, or was called with different arguments, the test fails.
One thing freshers mess up is trying to use verify to check return values. That's not what it does. You stub the return value with when then-return, then you assert on the result your code produces.
Verify just checks the call happened. I also wouldn't verify every single mock interaction — only the ones that are critical to the behavior being tested. Over-verifying makes tests brittle because they break whenever implementation details change, even if the behavior is still correct.
Mockito verify() Method | Java Fresher Interview
Direct Answer
verify(mock).method(args) asserts that method(args) was called exactly once on the mock. If not called, or called with different args, the test fails. This is behavior verification — checking that your code interacted with dependencies correctly.
Code Example
@ExtendWith(MockitoExtension.class)
class OrderServiceTest {
@Mock
private OrderRepository orderRepository;
@InjectMocks
private OrderService orderService;
@Test
void shouldSaveOrderWhenCreated() {
var order = new Order(1L, "Laptop", 1200.00);
orderService.createOrder(order);
verify(orderRepository).save(order); // asserts save() was called once with this order
}
@Test
void shouldNotifyCustomerAfterOrderSaved() {
var order = new Order(2L, "Phone", 800.00);
orderService.createOrder(order);
verify(orderRepository).save(any(Order.class)); // called with any Order
verify(notificationService).sendEmail(order.getCustomerId()); // email was sent
}
}
Common Mistakes & Pitfalls
- Verifying return values: Freshers often write verify(mock).getValue() and then try to assert on what it returned — but verify() doesn't return the method's result, it just checks the call happened. Use when().thenReturn() to stub the return value, then assert on the result your code produces.
- Over-verification: Adding verify() for every single mock interaction makes tests brittle. Only verify interactions that are critical to the behavior you're testing. If your service calls getUserById() to get data but the important part is what it does with that data, don't verify the getUserById() call.
- Wrong argument matchers: Using verify(mock).method(any()) when you actually care about specific arguments. If the exact value matters, pass it directly: verify(mock).method(expectedValue).
Check your understanding
Answer these without looking back at the article.
- What is the difference between verify() and assertions?
- How do you capture arguments passed to a mock?
- What is verifyNoMoreInteractions()?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.