Package org.mockito

Annotation Interface Mock


Mark a field as a mock.
  • Allows shorthand mock creation.
  • Minimizes repetitive mock creation code.
  • Makes the test class more readable.
  • Makes the verification error easier to read because the field name is used to identify the mock.
  • Automatically detects static mocks of type MockedStatic and infers the static mock type of the type parameter.

   public class ArticleManagerTest extends SampleBaseTestCase {

       @Mock private ArticleCalculator calculator;
       @Mock(name = "database") private ArticleDatabase dbMock;
       @Mock(answer = RETURNS_MOCKS) private UserProvider userProvider;
       @Mock(extraInterfaces = {Queue.class, Observer.class}) private ArticleMonitor articleMonitor;
       @Mock(stubOnly = true) private Logger logger;

       private ArticleManager manager;

       @Before public void setup() {
           manager = new ArticleManager(userProvider, database, calculator, articleMonitor, logger);
       }
   }

   public class SampleBaseTestCase {

       private AutoCloseable closeable;

       @Before public void openMocks() {
           closeable = MockitoAnnotations.openMocks(this);
       }

       @After public void releaseMocks() throws Exception {
           closeable.close();
       }
   }
 

MockitoAnnotations.openMocks(this) method has to be called to initialize annotated objects. In above example, openMocks() is called in @Before (JUnit4) method of test's base class. For JUnit3 openMocks() can go to setup() method of a base class. Instead you can also put openMocks() in your JUnit runner (@RunWith) or use the built-in MockitoJUnitRunner. Also, make sure to release any mocks after disposing your test class with a corresponding hook.

See Also:
  • Element Details

    • answer

      Answers answer
      Mock will have custom answer, see MockSettings.defaultAnswer(Answer). For examples how to use 'Mock' annotation and parameters see Mock.
      Default:
      RETURNS_DEFAULTS
    • stubOnly

      boolean stubOnly
      Mock will be 'stubOnly', see MockSettings.stubOnly(). For examples how to use 'Mock' annotation and parameters see Mock.
      Default:
      false
    • name

      String name
      Mock will have custom name (shown in verification errors), see MockSettings.name(String). For examples how to use 'Mock' annotation and parameters see Mock.
      Default:
      ""
    • extraInterfaces

      Class<?>[] extraInterfaces
      Mock will have extra interfaces, see MockSettings.extraInterfaces(Class[]). For examples how to use 'Mock' annotation and parameters see Mock.
      Default:
      {}
    • serializable

      boolean serializable
      Mock will be serializable, see MockSettings.serializable(). For examples how to use 'Mock' annotation and parameters see Mock.
      Default:
      false
    • lenient

      boolean lenient
      Mock will be lenient, see MockSettings.lenient(). For examples how to use 'Mock' annotation and parameters see Mock.
      Since:
      2.23.3
      Default:
      false