org.mockito.junit.jupiter.MockitoExtension Maven / Gradle / Ivy
/*
* Copyright (c) 2018 Mockito contributors
* This program is made available under the terms of the MIT License.
*/
package org.mockito.junit.jupiter;
import java.util.LinkedHashSet;
import java.util.Optional;
import java.util.Set;
import org.junit.jupiter.api.extension.AfterEachCallback;
import org.junit.jupiter.api.extension.BeforeEachCallback;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.junit.jupiter.api.extension.ExtensionContext.Namespace;
import org.junit.jupiter.api.extension.TestInstancePostProcessor;
import org.mockito.Mockito;
import org.mockito.MockitoSession;
import org.mockito.junit.MockitoJUnitRunner;
import org.mockito.quality.Strictness;
import static org.junit.jupiter.api.extension.ExtensionContext.Namespace.create;
import static org.junit.platform.commons.support.AnnotationSupport.findAnnotation;
/**
* Extension that initializes mocks and handles strict stubbings. This extension is the JUnit Jupiter equivalent
* of our JUnit4 {@link MockitoJUnitRunner}.
*
* Example usage:
*
*
* @ExtendWith(MockitoExtension.class)
* public class ExampleTest {
*
* @Mock
* private List list;
*
* @Test
* public void shouldDoSomething() {
* list.add(100);
* }
* }
*
*
* If you would like to configure the used strictness for the test class, use {@link MockitoSettings}.
*
*
* @MockitoSettings(strictness = Strictness.STRICT_STUBS)
* public class ExampleTest {
*
* @Mock
* private List list;
*
* @Test
* public void shouldDoSomething() {
* list.add(100);
* }
* }
*
*/
public class MockitoExtension implements TestInstancePostProcessor,BeforeEachCallback, AfterEachCallback {
private final static Namespace MOCKITO = create("org.mockito");
private final static String SESSION = "session";
private final static String TEST_INSTANCE = "testInstance";
private final Strictness strictness;
// This constructor is invoked by JUnit Jupiter via reflection or ServiceLoader
@SuppressWarnings("unused")
public MockitoExtension() {
this(Strictness.STRICT_STUBS);
}
private MockitoExtension(Strictness strictness) {
this.strictness = strictness;
}
/**
* Callback for post-processing the supplied test instance.
*
*
Note: the {@code ExtensionContext} supplied to a
* {@code TestInstancePostProcessor} will always return an empty
* {@link Optional} value from {@link ExtensionContext#getTestInstance()
* getTestInstance()}. A {@code TestInstancePostProcessor} should therefore
* only attempt to process the supplied {@code testInstance}.
*
* @param testInstance the instance to post-process; never {@code null}
* @param context the current extension context; never {@code null}
*/
@Override
public void postProcessTestInstance(Object testInstance, ExtensionContext context){
context.getStore(MOCKITO).put(TEST_INSTANCE, testInstance);
}
/**
* Callback that is invoked before each test is invoked.
*
* @param context the current extension context; never {@code null}
*/
@Override
public void beforeEach(final ExtensionContext context) {
Set