@Target(value={FIELD,PARAMETER}) @Retention(value=RUNTIME) @Documented public @interface Mock
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;
@Mock(stubOnly = true) private Logger logger;
private ArticleManager manager;
@Before public void setup() {
manager = new ArticleManager(userProvider, database, calculator, articleMonitor, logger);
}
}
public class SampleBaseTestCase {
@Before public void initMocks() {
MockitoAnnotations.initMocks(this);
}
}
MockitoAnnotations.initMocks(this)
method has to be called to initialize annotated objects.
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.
Instead you can also put initMocks() in your JUnit runner (@RunWith) or use the built-in
MockitoJUnitRunner
.
Modifier and Type | Optional Element and Description |
---|---|
Answers |
answer
Mock will have custom answer, see
MockSettings.defaultAnswer(Answer) . |
Class<?>[] |
extraInterfaces
Mock will have extra interfaces, see
MockSettings.extraInterfaces(Class[]) . |
boolean |
lenient
Mock will be lenient, see
MockSettings.lenient() . |
String |
name
Mock will have custom name (shown in verification errors), see
MockSettings.name(String) . |
boolean |
serializable
Mock will be serializable, see
MockSettings.serializable() . |
boolean |
stubOnly
Mock will be 'stubOnly', see
MockSettings.stubOnly() . |
public abstract Answers answer
MockSettings.defaultAnswer(Answer)
.
For examples how to use 'Mock' annotation and parameters see Mock
.public abstract boolean stubOnly
MockSettings.stubOnly()
.
For examples how to use 'Mock' annotation and parameters see Mock
.public abstract String name
MockSettings.name(String)
.
For examples how to use 'Mock' annotation and parameters see Mock
.public abstract Class<?>[] extraInterfaces
MockSettings.extraInterfaces(Class[])
.
For examples how to use 'Mock' annotation and parameters see Mock
.public abstract boolean serializable
MockSettings.serializable()
.
For examples how to use 'Mock' annotation and parameters see Mock
.public abstract boolean lenient
MockSettings.lenient()
.
For examples how to use 'Mock' annotation and parameters see Mock
.