public class MockitoExtension
extends java.lang.Object
implements org.junit.jupiter.api.extension.TestInstancePostProcessor, org.junit.jupiter.api.extension.BeforeEachCallback, org.junit.jupiter.api.extension.AfterEachCallback, org.junit.jupiter.api.extension.ParameterResolver
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 and Description |
---|
MockitoExtension() |
Modifier and Type | Method and Description |
---|---|
void |
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.
|
void |
postProcessTestInstance(java.lang.Object testInstance,
org.junit.jupiter.api.extension.ExtensionContext context)
Callback for post-processing the supplied test instance.
|
java.lang.Object |
resolveParameter(org.junit.jupiter.api.extension.ParameterContext parameterContext,
org.junit.jupiter.api.extension.ExtensionContext extensionContext) |
boolean |
supportsParameter(org.junit.jupiter.api.extension.ParameterContext parameterContext,
org.junit.jupiter.api.extension.ExtensionContext extensionContext) |
public void postProcessTestInstance(java.lang.Object testInstance, org.junit.jupiter.api.extension.ExtensionContext context)
Callback for post-processing the supplied test instance.
Note: the ExtensionContext
supplied to a
TestInstancePostProcessor
will always return an empty
Optional
value from getTestInstance()
. A TestInstancePostProcessor
should therefore
only attempt to process the supplied testInstance
.
postProcessTestInstance
in interface org.junit.jupiter.api.extension.TestInstancePostProcessor
testInstance
- the instance to post-process; never null
context
- the current extension context; never null
public void beforeEach(org.junit.jupiter.api.extension.ExtensionContext context)
beforeEach
in interface org.junit.jupiter.api.extension.BeforeEachCallback
context
- the current extension context; never null
public void afterEach(org.junit.jupiter.api.extension.ExtensionContext context)
afterEach
in interface org.junit.jupiter.api.extension.AfterEachCallback
context
- the current extension context; never null
public boolean supportsParameter(org.junit.jupiter.api.extension.ParameterContext parameterContext, org.junit.jupiter.api.extension.ExtensionContext extensionContext) throws org.junit.jupiter.api.extension.ParameterResolutionException
supportsParameter
in interface org.junit.jupiter.api.extension.ParameterResolver
org.junit.jupiter.api.extension.ParameterResolutionException
public java.lang.Object resolveParameter(org.junit.jupiter.api.extension.ParameterContext parameterContext, org.junit.jupiter.api.extension.ExtensionContext extensionContext) throws org.junit.jupiter.api.extension.ParameterResolutionException
resolveParameter
in interface org.junit.jupiter.api.extension.ParameterResolver
org.junit.jupiter.api.extension.ParameterResolutionException