Mockito Annotations In Unit Testing: A Complete Guide

Saumya - Aug 18 - - Dev Community

Mockito is a popular framework used in Java for unit testing and creating mock objects. Annotations in Mockito are used to simplify the process of creating mocks, stubs, and spies. Here’s a quick overview of some key Mockito annotations:

1. @Mock

The @Mock annotation is used to create and inject mocked instances. When you annotate a field with @Mock, Mockito creates a mock object for that field. For example:

java

@Mock
private MyService myService;

2. @InjectMocks

The @InjectMocks annotation is used to create an instance of the class under test and automatically inject the mocked dependencies (those created with @Mock or @spy) into it. For example:

java

@InjectMocks
private MyController myController;

3. @spy

The @spy annotation is used to create a spy object. Unlike mocks, spies are real objects that you can partially mock. This means that you can call real methods on the spy, but also stub specific methods to return mock values. For example:

java

@spy
private MyService myService;

4. @Captor

The @Captor annotation is used to create an argument captor. This helps in capturing arguments passed to mock methods for verification. For example:

java

@Captor
private ArgumentCaptor captor;

Example Usage

Here is a complete example demonstrating the use of these annotations:

java

@RunWith(MockitoJUnitRunner.class)
public class MyServiceTest {
    @Mock
    private DependencyService dependencyService;
    @InjectMocks
    private MyService myService;
    @Spy
    private AnotherService anotherService;
    @Captor
    private ArgumentCaptor<String> captor;
    @Test
    public void testServiceMethod() {
        // Setup mock behavior
        when(dependencyService.someMethod()).thenReturn("mocked value");

        // Call method under test
        myService.serviceMethod();

        // Verify interactions
        verify(dependencyService).someMethod();

        // Capture arguments
        verify(anotherService).anotherMethod(captor.capture());
        assertEquals("expected value", captor.getValue());
    }
}
Enter fullscreen mode Exit fullscreen mode

In this example:

@Mock creates a mock DependencyService instance.
@InjectMocks creates an instance of MyService and injects the mock DependencyService into it.
@spy creates a spy on AnotherService.
@Captor creates an ArgumentCaptor for capturing and asserting method arguments.

By understanding and effectively using these Mockito annotations, you can write more efficient and readable unit tests, making it easier to isolate and verify the behavior of the components in your Java application.

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Terabox Video Player