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
MockedStaticand 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(strictness = Mock.Strictness.LENIENT) private ArticleConsumer articleConsumer;
@Mock(stubOnly = true) private Logger logger;
private ArticleManager manager;
@Before public void setup() {
manager = new ArticleManager(userProvider, database, calculator, articleMonitor, articleConsumer, 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.
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:
-
Nested Class Summary
Nested Classes -
Optional Element Summary
Optional ElementsModifier and TypeOptional ElementDescriptionMock will have custom answer, seeMockSettings.defaultAnswer(Answer).Class<?>[]Mock will have extra interfaces, seeMockSettings.extraInterfaces(Class[]).booleanDeprecated.Mock will be created by the givenMockMaker, seeMockSettings.mockMaker(String).Mock will have custom name (shown in verification errors), seeMockSettings.name(String).booleanMock will be serializable, seeMockSettings.serializable().Mock will have custom strictness, seeMockSettings.strictness(org.mockito.quality.Strictness).booleanMock will be 'stubOnly', seeMockSettings.stubOnly().booleanMock will not attempt to preserve all annotation metadata, seeMockSettings.withoutAnnotations().
-
Element Details
-
answer
Answers answerMock will have custom answer, seeMockSettings.defaultAnswer(Answer). For examples how to use 'Mock' annotation and parameters seeMock.- Default:
RETURNS_DEFAULTS
-
stubOnly
boolean stubOnlyMock will be 'stubOnly', seeMockSettings.stubOnly(). For examples how to use 'Mock' annotation and parameters seeMock.- Default:
false
-
name
String nameMock will have custom name (shown in verification errors), seeMockSettings.name(String). For examples how to use 'Mock' annotation and parameters seeMock.- Default:
""
-
extraInterfaces
Class<?>[] extraInterfacesMock will have extra interfaces, seeMockSettings.extraInterfaces(Class[]). For examples how to use 'Mock' annotation and parameters seeMock.- Default:
{}
-
serializable
boolean serializableMock will be serializable, seeMockSettings.serializable(). For examples how to use 'Mock' annotation and parameters seeMock.- Default:
false
-
lenient
Deprecated.Usestrictness()instead. Mock will be lenient, seeMockSettings.lenient(). For examples how to use 'Mock' annotation and parameters seeMock.- Since:
- 2.23.3
- Default:
false
-
strictness
Mock.Strictness strictnessMock will have custom strictness, seeMockSettings.strictness(org.mockito.quality.Strictness). For examples how to use 'Mock' annotation and parameters seeMock.- Since:
- 4.6.1
- Default:
TEST_LEVEL_DEFAULT
-
mockMaker
String mockMakerMock will be created by the givenMockMaker, seeMockSettings.mockMaker(String).- Since:
- 4.8.0
- Default:
""
-
withoutAnnotations
boolean withoutAnnotationsMock will not attempt to preserve all annotation metadata, seeMockSettings.withoutAnnotations().- Since:
- 5.3.0
- Default:
false
-
strictness()instead.