Mockito Annotations

Learn about some of the annotations provided by Mockito to make tests shorter and more readable.

We'll cover the following

There are some Mockito annotations that help minimize the code for creating and injecting mocks. While writing tests, there can be some repetitive statements. We will use annotations provided by Mockito to get rid of the redundant code and make our tests more readable.

@Mock

The @Mock annotation is used to create a mock without calling the Mockito.mock() method.

Instead of creating a mock inside every test, we can move this statement outside and use the @Mock annotation on it. If we create mocks in the following way, we are using repetitive statements in every test:

Level up your interview prep. Join Educative to access 80+ hands-on prep courses.