Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
   * Copyright (c) 2007 Mockito contributors
   * This program is made available under the terms of the MIT License.
  package org.mockito;
  import static java.lang.annotation.ElementType.*;
  • Allows shorthand mock creation.
  • Minimizes repetitive mock creation code.
  • Makes the test class more readable.
  • Makes the verification error easier to read because field name is used to identify the mock.
   public class ArticleManagerTest extends SampleBaseTestCase { 
       @Mock private ArticleCalculator calculator;
       @Mock private ArticleDatabase database;
       @Mock private UserProvider userProvider;
       private ArticleManager manager;
       @Before public void setup() {
           manager = new ArticleManager(userProvider, database, calculator);
   public class SampleBaseTestCase {
       @Before public void initMocks() {
MockitoAnnotations.initMocks(this) method has to called to initialize annotated mocks.

In above example, initMocks() is called in @Before (JUnit4) method of test's base class. For JUnit3 initMocks() can go to setup() method of a base class. You can also put initMocks() in your JUnit runner (@RunWith) or use built-in runner: org.mockito.runners.MockitoJUnitRunner

 public class MockitoAnnotations {

Use top-level org.mockito.Mock annotation instead

When @Mock annotation was implemented as an inner class then users experienced problems with autocomplete features in IDEs. Hence @Mock was made a top-level class.

How to fix deprecation warnings? Typically, you can just search: import org.mockito.MockitoAnnotations.Mock; and replace with: import org.mockito.Mock;

If you're an existing user then sorry for making your code littered with deprecation warnings. This change was required to make Mockito better.

     @Target( {  })
     public @interface Mock {}
Initializes objects annotated with @Mock for given testClass.

See examples in javadoc for MockitoAnnotations class.

     public static void initMocks(Object testClass) {
         if (testClass == null) {
             throw new MockitoException("testClass cannot be null. For info how to use @Mock annotations see examples in javadoc for MockitoAnnotations class");
         Class<?> clazz = testClass.getClass();
         while (clazz != Object.class) {
             clazz = clazz.getSuperclass();
     private static void scan(Object testClassClass<?> clazz) {
         AnnotationEngine annotationEngine = new GlobalConfiguration().getAnnotationEngine();
         Field[] fields = clazz.getDeclaredFields();
         for (Field field : fields) {
             for(Annotation annotation : field.getAnnotations()) {
                 Object mock = annotationEngine.createMockFor(annotationfield);
                 if (mock != null) {
                     boolean wasAccessible = field.isAccessible();
                    try {
                    } catch (IllegalAccessException e) {
                        throw new MockitoException("Problems initiating mocks annotated with " + annotatione);
                    } finally {
New to GrepCode? Check out our FAQ X