Package org.mockito
Annotation Type Mock
-
@Target(FIELD) @Retention(RUNTIME) @Documented public @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.
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; private ArticleManager manager; @Before public void setup() { manager = new ArticleManager(userProvider, database, calculator, articleMonitor); } } 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 JUnit3initMocks()
can go tosetup()
method of a base class. Instead you can also put initMocks() in your JUnit runner (@RunWith) or use the built-inMockitoJUnitRunner
.
-
-
Optional Element Summary
Optional Elements Modifier and Type Optional Element Description Answers
answer
java.lang.Class<?>[]
extraInterfaces
java.lang.String
name
boolean
serializable
-
-
-
Element Detail
-
answer
Answers answer
- Default:
- org.mockito.Answers.RETURNS_DEFAULTS
-
-