Package org.mockito.junit.jupiter
Class MockitoExtension
java.lang.Object
org.mockito.junit.jupiter.MockitoExtension
- All Implemented Interfaces:
org.junit.jupiter.api.extension.AfterEachCallback
,org.junit.jupiter.api.extension.BeforeEachCallback
,org.junit.jupiter.api.extension.Extension
,org.junit.jupiter.api.extension.ParameterResolver
public class MockitoExtension
extends Object
implements org.junit.jupiter.api.extension.BeforeEachCallback, org.junit.jupiter.api.extension.AfterEachCallback, org.junit.jupiter.api.extension.ParameterResolver
Extension that initializes mocks and handles strict stubbings. This extension is the JUnit Jupiter equivalent
of our JUnit4
MockitoJUnitRunner
.
Example usage:
@ExtendWith(MockitoExtension.class)
public class ExampleTest {
@Mock
private List<Integer> list;
@Test
public void shouldDoSomething() {
list.add(100);
}
}
If you would like to configure the used strictness for the test class, use MockitoSettings
.
@MockitoSettings(strictness = Strictness.STRICT_STUBS)
public class ExampleTest {
@Mock
private List<Integer> list;
@Test
public void shouldDoSomething() {
list.add(100);
}
}
This extension also supports JUnit Jupiter's method parameters.
Use parameters for initialization of mocks that you use only in that specific test method.
In other words, where you would initialize local mocks in JUnit 4 by calling Mockito.mock(Class)
,
use the method parameter. This is especially beneficial when initializing a mock with generics, as you no
longer get a warning about "Unchecked assignment".
Please refer to JUnit Jupiter's documentation to learn when method parameters are useful.
@ExtendWith(MockitoExtension.class)
public class ExampleTest {
@Mock
private List<Integer> sharedList;
@Test
public void shouldDoSomething() {
sharedList.add(100);
}
@Test
public void hasLocalMockInThisTest(@Mock List<Integer> localList) {
localList.add(100);
sharedList.add(100);
}
}
Lastly, the extension supports JUnit Jupiter's constructor parameters.
This allows you to do setup work in the constructor and set
your fields to final
.
Please refer to JUnit Jupiter's documentation to learn when constructor parameters are useful.
@ExtendWith(MockitoExtension.class)
public class ExampleTest {
private final List<Integer> sharedList;
ExampleTest(@Mock sharedList) {
this.sharedList = sharedList;
}
@Test
public void shouldDoSomething() {
sharedList.add(100);
}
}
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionvoid
afterEach
(org.junit.jupiter.api.extension.ExtensionContext context) Callback that is invoked after each test has been invoked.void
beforeEach
(org.junit.jupiter.api.extension.ExtensionContext context) Callback that is invoked before each test is invoked.resolveParameter
(org.junit.jupiter.api.extension.ParameterContext parameterContext, org.junit.jupiter.api.extension.ExtensionContext context) boolean
supportsParameter
(org.junit.jupiter.api.extension.ParameterContext parameterContext, org.junit.jupiter.api.extension.ExtensionContext context)
-
Constructor Details
-
MockitoExtension
public MockitoExtension()
-
-
Method Details
-
beforeEach
public void beforeEach(org.junit.jupiter.api.extension.ExtensionContext context) Callback that is invoked before each test is invoked.- Specified by:
beforeEach
in interfaceorg.junit.jupiter.api.extension.BeforeEachCallback
- Parameters:
context
- the current extension context; nevernull
-
afterEach
public void afterEach(org.junit.jupiter.api.extension.ExtensionContext context) Callback that is invoked after each test has been invoked.- Specified by:
afterEach
in interfaceorg.junit.jupiter.api.extension.AfterEachCallback
- Parameters:
context
- the current extension context; nevernull
-
supportsParameter
public boolean supportsParameter(org.junit.jupiter.api.extension.ParameterContext parameterContext, org.junit.jupiter.api.extension.ExtensionContext context) throws org.junit.jupiter.api.extension.ParameterResolutionException - Specified by:
supportsParameter
in interfaceorg.junit.jupiter.api.extension.ParameterResolver
- Throws:
org.junit.jupiter.api.extension.ParameterResolutionException
-
resolveParameter
public Object resolveParameter(org.junit.jupiter.api.extension.ParameterContext parameterContext, org.junit.jupiter.api.extension.ExtensionContext context) throws org.junit.jupiter.api.extension.ParameterResolutionException - Specified by:
resolveParameter
in interfaceorg.junit.jupiter.api.extension.ParameterResolver
- Throws:
org.junit.jupiter.api.extension.ParameterResolutionException
-