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.AwesomeMockMakerthat extends the
mockito-extensions/org.mockito.plugins.MockMaker". The content of this file is exactly a one line with the qualified name:
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
|Modifier and Type||Interface and Description|
Carries the mockability information
|Modifier and Type||Method and Description|
If you want to provide your own implementation of
Returns the handler for the
Indicates if the given type can be mocked by this mockmaker.
Replaces the existing handler on
<T> T createMock(MockCreationSettings<T> settings, MockHandler handler)
MockMakerthis method should:
settings.typeToMockand potentially also
settingsto create/configure your proxy object.
handlerwith it. For example, if you generate byte code to create the proxy you could generate an extra field to keep the
handlerwith the generated object. Your implementation of
MockMakeris required to provide this instance of
T- Type of the mock to return, actually the
settings- Mock creation settings like type to mock, extra interfaces and so on.
MockHandler. Do not provide your own implementation at this time. Make sure your implementation of
getHandler(Object)will return this instance.
MockHandler getHandler(Object mock)
mock. Do not provide your own implementations at this time because the work on the
MockHandlerapi is not completed. Use the instance provided to you by Mockito at
resetMock(java.lang.Object, org.mockito.invocation.MockHandler, org.mockito.mock.MockCreationSettings).
mock- The mock instance.
void resetMock(Object mock, MockHandler newHandler, MockCreationSettings settings)
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- 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.