Class DefaultMockitoFramework
- All Implemented Interfaces:
MockitoFramework
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionaddListener
(MockitoListener listener) Adds listener to Mockito.void
clearInlineMock
(Object mock) Clears up internal state of specific inline mock.void
Clears up internal state of all inline mocks.private InlineMockMaker
Returns a factory that can create instances ofInvocation
.Returns an object that has access to Mockito plugins.removeListener
(MockitoListener listener) When you add listener usingMockitoFramework.addListener(MockitoListener)
make sure to remove it.
-
Constructor Details
-
DefaultMockitoFramework
public DefaultMockitoFramework()
-
-
Method Details
-
addListener
Description copied from interface:MockitoFramework
Adds listener to Mockito. For a list of supported listeners, see the interfaces that extendMockitoListener
.Listeners can be useful for components that extend Mockito framework. They are used in the implementation of unused stubbings warnings (
MockitoHint
).Make sure you remove the listener when the job is complete, see
MockitoFramework.removeListener(MockitoListener)
. Currently, the listeners list is thread local, so you need to remove listener from the same thread otherwise remove is ineffectual. In typical scenarios, it is not a problem, because adding and removing listeners typically happens in the same thread.If you are trying to add the listener but a listener of the same type was already added (and not removed) this method will throw
RedundantListenerException
. This is a safeguard to ensure users actually remove the listeners viaMockitoFramework.removeListener(MockitoListener)
. We do not anticipate the use case where adding the same listener type multiple times is useful. If this safeguard is problematic, please contact us via Mockito issue tracker.For usage examples, see Mockito codebase. If you have ideas and feature requests about Mockito listeners API we are very happy to hear about it via our issue tracker or mailing list.
Mockito.framework().addListener(myListener);
- Specified by:
addListener
in interfaceMockitoFramework
- Parameters:
listener
- to add to Mockito- Returns:
- this instance of mockito framework (fluent builder pattern)
-
removeListener
Description copied from interface:MockitoFramework
When you add listener usingMockitoFramework.addListener(MockitoListener)
make sure to remove it. Currently, the listeners list is thread local, so you need to remove listener from the same thread otherwise remove is ineffectual. In typical scenarios, it is not a problem, because adding and removing listeners typically happens in the same thread.For usage examples, see Mockito codebase. If you have ideas and feature requests about Mockito listeners API we are very happy to hear about it via our issue tracker or mailing list.
- Specified by:
removeListener
in interfaceMockitoFramework
- Parameters:
listener
- to remove- Returns:
- this instance of mockito framework (fluent builder pattern)
-
getPlugins
Description copied from interface:MockitoFramework
Returns an object that has access to Mockito plugins. An example plugin isMockMaker
. For information why and how to use this method seeMockitoPlugins
.- Specified by:
getPlugins
in interfaceMockitoFramework
- Returns:
- object that gives access to mockito plugins
-
getInvocationFactory
Description copied from interface:MockitoFramework
Returns a factory that can create instances ofInvocation
. It is useful for framework integrations, becauseInvocation
isNotExtensible
.- Specified by:
getInvocationFactory
in interfaceMockitoFramework
- Returns:
- object that can construct invocations
-
getInlineMockMaker
-
clearInlineMocks
public void clearInlineMocks()Description copied from interface:MockitoFramework
Clears up internal state of all inline mocks. This method is only meaningful if inline mock maker is in use. For all other intents and purposes, this method is a no-op and need not be used.This method is useful to tackle subtle memory leaks that are possible due to the nature of inline mocking (issue #1619). If you are facing those problems, call this method at the end of the test (or in "@After" method). See examples of using "clearInlineMocks" in Mockito test code. To find out why inline mock maker keeps track of the mock objects see
InlineMockMaker
.Mockito's "inline mocking" enables mocking final types, enums and final methods (read more in section 39 of
Mockito
javadoc). This method is only meaningful whenInlineMockMaker
is in use. If you're using a differentMockMaker
then this method is a no-op.
If you have feedback or a better idea how to solve the problem please reach out.public class ExampleTest { @After public void clearMocks() { Mockito.framework().clearInlineMocks(); } @Test public void someTest() { ... } }
- Specified by:
clearInlineMocks
in interfaceMockitoFramework
- See Also:
-
clearInlineMock
Description copied from interface:MockitoFramework
Clears up internal state of specific inline mock. This method is a single-mock variant ofMockitoFramework.clearInlineMocks()
. Please read javadoc forMockitoFramework.clearInlineMocks()
.- Specified by:
clearInlineMock
in interfaceMockitoFramework
- Parameters:
mock
- to clear up- See Also:
-