Package org.mockito.internal.framework
Class DefaultMockitoSession
java.lang.Object
org.mockito.internal.framework.DefaultMockitoSession
- All Implemented Interfaces:
MockitoSession
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final List<AutoCloseable>
private final UniversalTestListener
private final String
-
Constructor Summary
ConstructorsConstructorDescriptionDefaultMockitoSession
(List<Object> testClassInstances, String name, Strictness strictness, MockitoLogger logger) -
Method Summary
Modifier and TypeMethodDescriptionvoid
Must be invoked when the user is done with mocking for given session (test method).void
finishMocking
(Throwable failure) Must be invoked when the user is done with mocking for given session (test method).private void
release()
void
setStrictness
(Strictness strictness) Changes the strictness of thisMockitoSession
.
-
Field Details
-
name
-
listener
-
closeables
-
-
Constructor Details
-
DefaultMockitoSession
public DefaultMockitoSession(List<Object> testClassInstances, String name, Strictness strictness, MockitoLogger logger)
-
-
Method Details
-
setStrictness
Description copied from interface:MockitoSession
Changes the strictness of thisMockitoSession
. The new strictness will be applied to operations on mocks and checks performed byMockitoSession.finishMocking()
. This method is used behind the hood byMockitoRule.strictness(Strictness)
method. In most healthy tests, this method is not needed. We keep it for edge cases and when you really need to change strictness in given test method. For use cases see Javadoc forPotentialStubbingProblem
class.- Specified by:
setStrictness
in interfaceMockitoSession
- Parameters:
strictness
- new strictness for this session.
-
finishMocking
public void finishMocking()Description copied from interface:MockitoSession
Must be invoked when the user is done with mocking for given session (test method). It detects unused stubbings and may throwUnnecessaryStubbingException
or emit warnings (MockitoHint
) depending on theStrictness
level. The method also detects incorrect Mockito usage viaMockito.validateMockitoUsage()
.In order to implement
Strictness
Mockito session keeps track of mocking usingMockitoListener
. This method cleans up the listeners and ensures there is no leftover state after the session finishes. It is necessary to invoke this method to conclude mocking session. For more information about session lifecycle seeMockitoSessionBuilder.startMocking()
.This method is intended to be used in your test framework's 'tear down' method. In the case of JUnit it is the "@After" method.
For example, see javadoc for
MockitoSession
.- Specified by:
finishMocking
in interfaceMockitoSession
- See Also:
-
finishMocking
Description copied from interface:MockitoSession
Must be invoked when the user is done with mocking for given session (test method). When a failure is specified, certain checks are disabled to avoid confusion that may arise because there are multiple competing failures. Other than that, this method behaves exactly likeMockitoSession.finishMocking()
.This method is intended to be used by framework integrations. When using MockitoSession directly, most users should rather use
MockitoSession.finishMocking()
.MockitoRule
uses this method behind the hood.- Specified by:
finishMocking
in interfaceMockitoSession
- Parameters:
failure
- the exception that caused the test to fail; passingnull
is permitted- See Also:
-
release
private void release()
-