Interface MockitoSessionBuilder
-
@NotExtensible public interface MockitoSessionBuilder
Fluent builder interface forMockitoSession
objects. See the documentation and examples in Javadoc forMockitoSession
.- Since:
- 2.7.0
-
-
Method Summary
All Methods Instance Methods Abstract Methods Modifier and Type Method Description MockitoSessionBuilder
initMocks(Object testClassInstance)
Adds the test class instance for initialization of fields annotated with Mockito annotations likeMock
.MockitoSessionBuilder
initMocks(Object... testClassInstances)
Adds the test class instances for initialization of fields annotated with Mockito annotations likeMock
.MockitoSessionBuilder
logger(MockitoSessionLogger logger)
Configures logger used byMockitoSession
for emitting warnings when finishing the session.MockitoSessionBuilder
name(String name)
Configures the name of theMockitoSession
instance.MockitoSession
startMocking()
Starts new mocking session! Creates newMockitoSession
instance to initialize the session.MockitoSessionBuilder
strictness(Strictness strictness)
Configures strictness ofMockitoSession
instance.
-
-
-
Method Detail
-
initMocks
MockitoSessionBuilder initMocks(Object testClassInstance)
Adds the test class instance for initialization of fields annotated with Mockito annotations likeMock
. When this method is invoked it does not perform initialization of mocks on the spot! Only whenstartMocking()
is invoked then annotated fields will be initialized. Traditional API to initialize mocks, theMockitoAnnotations.openMocks(Object)
method has limited support for driving cleaner tests because it does not support configuringStrictness
. Want cleaner tests and better productivity? Migrate fromMockitoAnnotations.openMocks(Object)
toMockitoSession
!This method may be called multiple times to add multiple, e.g. nested, test class instances.
See code sample in
MockitoSession
.- Parameters:
testClassInstance
- test class instance that contains fields with Mockito annotations to be initialized. Passingnull
is permitted but will be ignored.- Returns:
- the same builder instance for fluent configuration of
MockitoSession
. - Since:
- 2.7.0
-
initMocks
MockitoSessionBuilder initMocks(Object... testClassInstances)
Adds the test class instances for initialization of fields annotated with Mockito annotations likeMock
.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.- Parameters:
testClassInstances
- test class instances that contains fields with Mockito annotations to be initialized. Passingnull
or an empty array is permitted but will be ignored.- Returns:
- the same builder instance for fluent configuration of
MockitoSession
. - Since:
- 2.15.0
- See Also:
initMocks(Object)
-
name
MockitoSessionBuilder name(String name)
Configures the name of theMockitoSession
instance.The name is used to output hints when finishing a session.
This method is intended to be used by framework integrations, e.g. JUnit. When building a
MockitoSession
for direct use, users are not expected to call it.- Parameters:
name
- ofMockitoSession
instance. Passingnull
is permitted and will make the session use a default value. The current default is the name of the last test class instance passed toinitMocks(Object)
orinitMocks(Object...)
, if available; otherwise,"<Unnamed Session>"
is used.- Returns:
- the same builder instance for fluent configuration of
MockitoSession
. - Since:
- 2.15.0
- See Also:
MockitoHint
-
strictness
MockitoSessionBuilder strictness(Strictness strictness)
Configures strictness ofMockitoSession
instance. See examples inMockitoSession
.- Parameters:
strictness
- forMockitoSession
instance. Passingnull
is permitted and will make the session use a default value. The current default isStrictness.STRICT_STUBS
.- Returns:
- the same builder instance for fluent configuration of
MockitoSession
. - Since:
- 2.7.0
-
logger
MockitoSessionBuilder logger(MockitoSessionLogger logger)
Configures logger used byMockitoSession
for 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
strictness(Strictness)
.- Parameters:
logger
- for warnings emitted when finishingMockitoSession
. Passingnull
is permitted and will make the session use a default value. By default, warnings will be logged to the console.- Returns:
- the same builder instance for fluent configuration of
MockitoSession
. - Since:
- 2.15.0
- See Also:
MockitoHint
-
startMocking
MockitoSession startMocking() throws UnfinishedMockingSessionException
Starts new mocking session! Creates newMockitoSession
instance to initialize the session. At this point annotated fields are initialized perinitMocks(Object)
method. When you are done with the session it is required to invokeMockitoSession.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 using
MockitoSession.finishMocking()
. 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 sessionUnfinishedMockingSessionException
will be triggered.See examples in
MockitoSession
.- Returns:
- new
MockitoSession
instance - Throws:
UnfinishedMockingSessionException
- when previous session was not concluded withMockitoSession.finishMocking()
- Since:
- 2.7.0
-
-