Package org.mockito.internal.framework
Class DefaultMockitoSession
- java.lang.Object
-
- org.mockito.internal.framework.DefaultMockitoSession
-
- All Implemented Interfaces:
MockitoSession
public class DefaultMockitoSession extends java.lang.Object implements MockitoSession
-
-
Field Summary
Fields Modifier and Type Field Description private java.util.List<java.lang.AutoCloseable>
closeables
private UniversalTestListener
listener
private java.lang.String
name
-
Constructor Summary
Constructors Constructor Description DefaultMockitoSession(java.util.List<java.lang.Object> testClassInstances, java.lang.String name, Strictness strictness, MockitoLogger logger)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
finishMocking()
Must be invoked when the user is done with mocking for given session (test method).void
finishMocking(java.lang.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 Detail
-
name
private final java.lang.String name
-
listener
private final UniversalTestListener listener
-
closeables
private final java.util.List<java.lang.AutoCloseable> closeables
-
-
Constructor Detail
-
DefaultMockitoSession
public DefaultMockitoSession(java.util.List<java.lang.Object> testClassInstances, java.lang.String name, Strictness strictness, MockitoLogger logger)
-
-
Method Detail
-
setStrictness
public void setStrictness(Strictness strictness)
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:
MockitoSession.finishMocking(Throwable)
-
finishMocking
public void finishMocking(java.lang.Throwable failure)
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:
MockitoSession.finishMocking()
-
release
private void release()
-
-