org.mockito.MockitoAnnotations Maven / Gradle / Ivy
Show all versions of mockito-core Show documentation
/*
* Copyright (c) 2007 Mockito contributors
* This program is made available under the terms of the MIT License.
*/
package org.mockito;
import static org.mockito.internal.util.StringUtil.join;
import org.mockito.exceptions.base.MockitoException;
import org.mockito.internal.configuration.GlobalConfiguration;
import org.mockito.junit.MockitoJUnitRunner;
import org.mockito.plugins.AnnotationEngine;
/**
* MockitoAnnotations.openMocks(this); initializes fields annotated with Mockito annotations.
* See also {@link MockitoSession} which not only initializes mocks
* but also adds extra validation for cleaner tests!
*
*
* - Allows shorthand creation of objects required for testing.
* - Minimizes repetitive mock creation code.
* - Makes the test class more readable.
* - Makes the verification error easier to read because field name is used to identify the mock.
*
*
*
* 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 {
*
* private AutoCloseable closeable;
*
* @Before public void openMocks() {
* closeable = MockitoAnnotations.openMocks(this);
* }
*
* @After public void releaseMocks() throws Exception {
* closeable.close();
* }
* }
*
*
* Read also about other annotations @{@link Spy}, @{@link Captor}, @{@link InjectMocks}
*
* MockitoAnnotations.openMocks(this)
method has to be called to initialize annotated fields.
*
* In above example, openMocks()
is called in @Before (JUnit4) method of test's base class.
* You can also put openMocks() in your JUnit runner (@RunWith) or use built-in runner: {@link MockitoJUnitRunner}.
* If static method mocks are used, it is required to close the initialization. Additionally, if using third-party
* mock-makers, other life-cycles might be handled by the open-release routine.
*/
public final class MockitoAnnotations {
/**
* Initializes objects annotated with Mockito annotations for given testClass:
* @{@link org.mockito.Mock}, @{@link Spy}, @{@link Captor}, @{@link InjectMocks}
*
* See examples in javadoc for {@link MockitoAnnotations} class.
*
* @return A closable to close when completing any tests in {@code testClass}.
*/
public static AutoCloseable openMocks(Object testClass) {
if (testClass == null) {
throw new MockitoException(
"testClass cannot be null. For info how to use @Mock annotations see examples in javadoc for MockitoAnnotations class");
}
AnnotationEngine annotationEngine =
new GlobalConfiguration().tryGetPluginAnnotationEngine();
return annotationEngine.process(testClass.getClass(), testClass);
}
/**
* Initializes objects annotated with Mockito annotations for given testClass:
* @{@link org.mockito.Mock}, @{@link Spy}, @{@link Captor}, @{@link InjectMocks}
*
* See examples in javadoc for {@link MockitoAnnotations} class.
*
* @deprecated Use {@link MockitoAnnotations#openMocks(Object)} instead.
* This method is equivalent to {@code openMocks(testClass).close()}.
* The close method should however only be called after completed usage of {@code testClass}.
* If using static-mocks or custom {@link org.mockito.plugins.MockMaker}s, using this method might
* cause misbehavior of mocks injected into the test class.
*/
@Deprecated
public static void initMocks(Object testClass) {
try {
openMocks(testClass).close();
} catch (Exception e) {
throw new MockitoException(
join(
"Failed to release mocks",
"",
"This should not happen unless you are using a third-party mock maker"),
e);
}
}
private MockitoAnnotations() {}
}