What is the purpose of the @Captor annotation in Mockito?

What is the purpose of the @Captor annotation in Mockito?

A) To capture argument values passed to mock methods
B) To create mock objects
C) To inject mocks into the test
D) To verify the number of method calls

Answer:

A) To capture argument values passed to mock methods

Explanation:

The @Captor annotation in Mockito is used to capture argument values passed to mock methods. It allows you to access and verify the arguments that were passed to a method on a mock during the test.

This is particularly useful when you want to perform more detailed assertions on the arguments, beyond just verifying that the method was called.

For example:


@Captor
ArgumentCaptor<String> captor;

verify(mockObject).someMethod(captor.capture());
assertEquals("expectedValue", captor.getValue());

In this example, the @Captor annotation is used to create an ArgumentCaptor that captures the argument passed to someMethod(). The captured value can then be asserted to check if it matches the expected value.

Reference links:

https://www.javaguides.net/p/mockito-tutorial.html

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top