What are the different ways to create a mock in Mockito?

Beginner

Answer

There are several ways to create mocks in Mockito:

  1. Using mock() method:
List<String> mockList = mock(List.class);
  1. Using @Mock annotation:
@Mock
private List<String> mockList;
  1. Using @MockBean (Spring Boot):
@MockBean
private UserService userService;
  1. Using MockitoAnnotations.openMocks():
@BeforeEach
void setUp() {
    MockitoAnnotations.openMocks(this);
}

The annotation approach is preferred as it makes tests cleaner and more readable.