Test Overview
This test uses the @Mock annotation to create a mock object for a dependency. It verifies that the method under test calls the mocked dependency correctly and returns the expected result.
This test uses the @Mock annotation to create a mock object for a dependency. It verifies that the method under test calls the mocked dependency correctly and returns the expected result.
import static org.mockito.Mockito.*; import static org.junit.jupiter.api.Assertions.*; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.Mock; import org.mockito.junit.jupiter.MockitoExtension; @ExtendWith(MockitoExtension.class) public class UserServiceTest { @Mock UserRepository userRepository; @Test void testGetUserName() { // Arrange UserService userService = new UserService(userRepository); when(userRepository.findNameById(1)).thenReturn("Alice"); // Act String name = userService.getUserName(1); // Assert assertEquals("Alice", name); verify(userRepository).findNameById(1); } } class UserService { private final UserRepository userRepository; public UserService(UserRepository userRepository) { this.userRepository = userRepository; } public String getUserName(int id) { return userRepository.findNameById(id); } } interface UserRepository { String findNameById(int id); }
| Step | Action | System State | Assertion | Result |
|---|---|---|---|---|
| 1 | Test starts and MockitoExtension initializes the test class | UserServiceTest instance created with userRepository mocked | — | PASS |
| 2 | UserService instance created with mocked userRepository | UserService ready to use mocked dependency | — | PASS |
| 3 | Mock userRepository is stubbed to return 'Alice' when findNameById(1) is called | Mock behavior set | — | PASS |
| 4 | Call getUserName(1) on UserService | UserService calls mocked userRepository.findNameById(1) | — | PASS |
| 5 | Verify returned name is 'Alice' | Result from getUserName is 'Alice' | assertEquals("Alice", name) | PASS |
| 6 | Verify userRepository.findNameById(1) was called at least once | Mock interaction recorded | verify(userRepository).findNameById(1) | PASS |
| 7 | Test ends successfully | All assertions passed | — | PASS |
@Mock annotation in JUnit testing?@Mock@Mock annotation is used to create fake objects that imitate real dependencies in tests.@Mock creates fake objects [OK]@Mock in a JUnit 5 test class?@Mock private MyService service; without initializing it.service.getData() when called inside the test?import static org.mockito.Mockito.*;
import org.junit.jupiter.api.Test;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
class MyServiceTest {
@Mock
MyService service;
@Test
void testGetData() {
MockitoAnnotations.openMocks(this);
when(service.getData()).thenReturn("mocked data");
System.out.println(service.getData());
}
}MockitoAnnotations.openMocks(this) initializes the @Mock annotated fields.when(service.getData()).thenReturn("mocked data") sets the mock to return "mocked data" when getData() is called.@Mock:import org.junit.jupiter.api.Test;
import org.mockito.Mock;
class UserServiceTest {
@Mock
UserRepository repo;
@Test
void testFindUser() {
when(repo.findUserById(1)).thenReturn(new User(1, "Alice"));
UserService service = new UserService(repo);
User user = service.findUser(1);
assertEquals("Alice", user.getName());
}
}@Mock UserRepository repo; but never initializes mocks with MockitoAnnotations.openMocks(this) or similar.repo is null, so calling when(repo.findUserById(1)) causes a NullPointerException.@Mock to create fake objects for both. Which of the following is the best practice to ensure your mocks work correctly in the test?@Mock must be initialized before use, typically by calling MockitoAnnotations.openMocks(this) in a setup method.new is incorrect; real instances break isolation; local variables lose scope and initialization benefits.MockitoAnnotations.openMocks(this) in a setup method -> Option BPass the test and this pattern moves unknown