|Modifier and Type||Method and Description|
Adds the test class instances for initialization of fields annotated with Mockito annotations like
Adds the test class instance for initialization of fields annotated with Mockito annotations like
Configures logger used by
Configures the name of the
Starts new mocking session! Creates new
Configures strictness of
@Incubating MockitoSessionBuilder initMocks(Object testClassInstance)
Mock. When this method is invoked it does not perform initialization of mocks on the spot! Only when
startMocking()is invoked then annotated fields will be initialized. Traditional API to initialize mocks, the
MockitoAnnotations.initMocks(Object)method has limited support for driving cleaner tests because it does not support configuring
Strictness. Want cleaner tests and better productivity? Migrate from
This method may be called multiple times to add multiple, e.g. nested, test class instances.
See code sample in
testClassInstance- test class instance that contains fields with Mockito annotations to be initialized. Passing
nullis permitted but will be ignored.
@Incubating MockitoSessionBuilder initMocks(Object... testClassInstances)
In most scenarios, you only need to init mocks on a single test class instance. This method is useful for advanced framework integrations (like JUnit Jupiter), when a test uses multiple, e.g. nested, test class instances.
This method calls
initMocks(Object) for each passed test class instance.
testClassInstances- test class instances that contains fields with Mockito annotations to be initialized. Passing
nullor an empty array is permitted but will be ignored.
@Incubating MockitoSessionBuilder name(String name)
This method is intended to be used by framework integrations, e.g. JUnit. When building
MockitoSession for direct use, users are not expected to call it.
nullis permitted and will make the session use a default value. The current default is the name of the last test class instance passed to
initMocks(Object...), if available; otherwise,
"<Unnamed Session>"is used.
@Incubating MockitoSessionBuilder strictness(Strictness strictness)
MockitoSessioninstance. See examples in
nullis permitted and will make the session use a default value. The current default is
@Incubating MockitoSessionBuilder logger(MockitoSessionLogger logger)
MockitoSessionfor emitting warnings when finishing the session.
Please note that the use of strict stubs is
recommended over emitting warnings because warnings are easily ignored and spoil the log output.
Instead of using this method, please consider setting strictness with
logger- for warnings emitted when finishing
nullis permitted and will make the session use a default value. By default, warnings will be logged to the console.
@Incubating MockitoSession startMocking() throws UnfinishedMockingSessionException
MockitoSessioninstance to initialize the session. At this point annotated fields are initialized per
initMocks(Object)method. When you are done with the session it is required to invoke
MockitoSession.finishMocking(). This will trigger stubbing validation, cleaning up the internal state like removal of internal listeners.
Mockito tracks created sessions internally and prevents the user from creating new sessions without
When you run tests concurrently in multiple threads, it is legal for each thread to have single active Mockito session.
When you attempt to start new session in a thread that already has an unfinished session
UnfinishedMockingSessionException will be triggered.
See examples in