public interface MockMaker
By default, an internal byte-buddy/asm/objenesis based implementation is used.
MockMaker
is an extension point that makes it possible to use custom dynamic proxies
and avoid using the default byte-buddy/asm/objenesis implementation.
For example, the android users can use a MockMaker that can work with Dalvik virtual machine
and hence bring Mockito to android apps developers.
Suppose you wrote an extension to create mocks with some Awesome library, in order to tell Mockito to use it you need to put in your classpath:
org.awesome.mockito.AwesomeMockMaker
that
extends the MockMaker
.
mockito-extensions/org.mockito.plugins.MockMaker
". The content of this file is
exactly a one line with the qualified name:
org.awesome.mockito.AwesomeMockMaker
.
Note that if several mockito-extensions/org.mockito.plugins.MockMaker
files exists in the classpath
Mockito will only use the first returned by the standard ClassLoader.getResource(java.lang.String)
mechanism.
MockCreationSettings
,
MockHandler
Modifier and Type | Interface and Description |
---|---|
static interface |
MockMaker.ConstructionMockControl<T> |
static interface |
MockMaker.StaticMockControl<T> |
static interface |
MockMaker.TypeMockability
Carries the mockability information
|
Modifier and Type | Method and Description |
---|---|
default void |
clearAllCaches()
Clears all cashes for mocked types and removes all byte code alterations, if possible.
|
default <T> MockMaker.ConstructionMockControl<T> |
createConstructionMock(Class<T> type,
java.util.function.Function<MockedConstruction.Context,MockCreationSettings<T>> settingsFactory,
java.util.function.Function<MockedConstruction.Context,MockHandler<T>> handlerFactory,
MockedConstruction.MockInitializer<T> mockInitializer)
If you want to provide your own implementation of
MockMaker this method should:
Intercept all constructions of the specified type in the current thread
Only intercept the construction after being enabled.
Stops the interception when disabled.
|
<T> T |
createMock(MockCreationSettings<T> settings,
MockHandler handler)
If you want to provide your own implementation of
MockMaker this method should:
Create a proxy object that implements settings.typeToMock and potentially also settings.extraInterfaces .
You may use the information from settings to create/configure your proxy object.
Your proxy object should carry the handler with it. |
default <T> Optional<T> |
createSpy(MockCreationSettings<T> settings,
MockHandler handler,
T instance)
By implementing this method, a mock maker can optionally support the creation of spies where all fields
are set within a constructor.
|
default <T> MockMaker.StaticMockControl<T> |
createStaticMock(Class<T> type,
MockCreationSettings<T> settings,
MockHandler handler)
If you want to provide your own implementation of
MockMaker this method should:
Alter the supplied class to only change its behavior in the current thread.
Only alters the static method's behavior after being enabled.
Stops the altered behavior when disabled.
|
MockHandler |
getHandler(Object mock)
Returns the handler for the
mock . |
MockMaker.TypeMockability |
isTypeMockable(Class<?> type)
Indicates if the given type can be mocked by this mockmaker.
|
void |
resetMock(Object mock,
MockHandler newHandler,
MockCreationSettings settings)
Replaces the existing handler on
mock with newHandler . |
<T> T createMock(MockCreationSettings<T> settings, MockHandler handler)
MockMaker
this method should:
settings.typeToMock
and potentially also settings.extraInterfaces
.settings
to create/configure your proxy object.handler
with it. For example, if you generate byte code
to create the proxy you could generate an extra field to keep the handler
with the generated object.
Your implementation of MockMaker
is required to provide this instance of handler
when
getHandler(Object)
is called.
T
- Type of the mock to return, actually the settings.getTypeToMock
.settings
- Mock creation settings like type to mock, extra interfaces and so on.handler
- See MockHandler
.
Do not provide your own implementation at this time. Make sure your implementation of
getHandler(Object)
will return this instance.default <T> Optional<T> createSpy(MockCreationSettings<T> settings, MockHandler handler, T instance)
T
- Type of the mock to return, actually the settings.getTypeToMock
.settings
- Mock creation settings like type to mock, extra interfaces and so on.handler
- See MockHandler
.
Do not provide your own implementation at this time. Make sure your implementation of
getHandler(Object)
will return this instance.instance
- The object to spy upon.MockHandler getHandler(Object mock)
mock
. Do not provide your own implementations at this time
because the work on the MockHandler
api is not completed.
Use the instance provided to you by Mockito at createMock(org.mockito.mock.MockCreationSettings<T>, org.mockito.invocation.MockHandler)
or resetMock(java.lang.Object, org.mockito.invocation.MockHandler, org.mockito.mock.MockCreationSettings)
.mock
- The mock instance.void resetMock(Object mock, MockHandler newHandler, MockCreationSettings settings)
mock
with newHandler
.
The invocation handler actually store invocations to achieve stubbing and verification. In order to reset the mock, we pass a new instance of the invocation handler.
Your implementation should make sure the newHandler
is correctly associated to passed mock
mock
- The mock instance whose invocation handler is to be replaced.newHandler
- The new invocation handler instance.settings
- The mock settings - should you need to access some of the mock creation details.@Incubating MockMaker.TypeMockability isTypeMockable(Class<?> type)
Mockmaker may have different capabilities in term of mocking, typically Mockito 1.x's internal mockmaker cannot mock final types. Other implementations, may have different limitations.
type
- The type inspected for mockability.@Incubating default <T> MockMaker.StaticMockControl<T> createStaticMock(Class<T> type, MockCreationSettings<T> settings, MockHandler handler)
MockMaker
this method should:
T
- Type of the mock to return, actually the settings.getTypeToMock
.settings
- Mock creation settings like type to mock, extra interfaces and so on.handler
- See MockHandler
.
Do not provide your own implementation at this time. Make sure your implementation of
getHandler(Object)
will return this instance.@Incubating default <T> MockMaker.ConstructionMockControl<T> createConstructionMock(Class<T> type, java.util.function.Function<MockedConstruction.Context,MockCreationSettings<T>> settingsFactory, java.util.function.Function<MockedConstruction.Context,MockHandler<T>> handlerFactory, MockedConstruction.MockInitializer<T> mockInitializer)
MockMaker
this method should:
T
- Type of the mock to return, actually the settings.getTypeToMock
.settingsFactory
- Factory for mock creation settings like type to mock, extra interfaces and so on.handlerFactory
- Factory for settings. See MockHandler
.
Do not provide your own implementation at this time. Make sure your implementation of
getHandler(Object)
will return this instance.@Incubating default void clearAllCaches()