
org.mockito.Mock Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of mockito-all Show documentation
Show all versions of mockito-all Show documentation
Mock objects library for java, modified to allow cache disabling
The newest version!
/*
* Copyright (c) 2007 Mockito contributors
* This program is made available under the terms of the MIT License.
*/
package org.mockito;
import org.mockito.runners.MockitoJUnitRunner;
import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import static java.lang.annotation.ElementType.FIELD;
/**
*
* - 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 = "dbMock") private ArticleDatabase database;
* @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 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 runners: {@link MockitoJUnitRunner}
*/
@Target( { FIELD })
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Mock {
Answers answer() default Answers.RETURNS_DEFAULTS;
String name() default "";
Class>[] extraInterfaces() default {};
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy