public class MockitoAnnotations extends Object
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);
}
}
Read also about other annotations @Spy, @Captor, @InjectMocks
MockitoAnnotations.initMocks(this) method has to called to initialize annotated fields.
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: MockitoJUnitRunner
| Modifier and Type | Class and Description |
|---|---|
static interface |
MockitoAnnotations.Mock
Deprecated.
|
| Constructor and Description |
|---|
MockitoAnnotations() |
| Modifier and Type | Method and Description |
|---|---|
static void |
initMocks(Object testClass)
Initializes objects annotated with Mockito annotations for given testClass:
@
Mock, @Spy, @Captor, @InjectMocks |
public static void initMocks(Object testClass)
Mock, @Spy, @Captor, @InjectMocks
See examples in javadoc for MockitoAnnotations class.
Copyright © 2018. All rights reserved.