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>closeablesprivate UniversalTestListenerlistenerprivate java.lang.Stringname
-
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 voidfinishMocking()Must be invoked when the user is done with mocking for given session (test method).voidfinishMocking(java.lang.Throwable failure)Must be invoked when the user is done with mocking for given session (test method).private voidrelease()voidsetStrictness(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:MockitoSessionChanges 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 forPotentialStubbingProblemclass.- Specified by:
setStrictnessin interfaceMockitoSession- Parameters:
strictness- new strictness for this session.
-
finishMocking
public void finishMocking()
Description copied from interface:MockitoSessionMust be invoked when the user is done with mocking for given session (test method). It detects unused stubbings and may throwUnnecessaryStubbingExceptionor emit warnings (MockitoHint) depending on theStrictnesslevel. The method also detects incorrect Mockito usage viaMockito.validateMockitoUsage().In order to implement
StrictnessMockito 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:
finishMockingin interfaceMockitoSession- See Also:
MockitoSession.finishMocking(Throwable)
-
finishMocking
public void finishMocking(java.lang.Throwable failure)
Description copied from interface:MockitoSessionMust 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().MockitoRuleuses this method behind the hood.- Specified by:
finishMockingin interfaceMockitoSession- Parameters:
failure- the exception that caused the test to fail; passingnullis permitted- See Also:
MockitoSession.finishMocking()
-
release
private void release()
-
-