Interface MockMaker
-
- All Known Implementing Classes:
CglibMockMaker
public interface MockMaker
The facility to create mocks.By default, an internal cglib/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 cglib/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.Using the extension point
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:
- The implementation itself, for example
org.awesome.mockito.AwesomeMockMaker
that extends theMockMaker
. - A file "
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 standardClassLoader.getResource(java.lang.String)
mechanism.- Since:
- 1.9.5
- See Also:
MockCreationSettings
,MockHandler
-
-
Method Summary
All Methods Instance Methods Abstract Methods Modifier and Type Method Description <T> T
createMock(MockCreationSettings<T> settings, MockHandler handler)
If you want to provide your own implementation ofMockMaker
this method should: Create a proxy object that implementssettings.typeToMock
and potentially alsosettings.extraInterfaces
. You may use the information fromsettings
to create/configure your proxy object. Your proxy object should carry thehandler
with it.MockHandler
getHandler(java.lang.Object mock)
Returns the handler for themock
.void
resetMock(java.lang.Object mock, MockHandler newHandler, MockCreationSettings settings)
Replaces the existing handler onmock
withnewHandler
.
-
-
-
Method Detail
-
createMock
<T> T createMock(MockCreationSettings<T> settings, MockHandler handler)
If you want to provide your own implementation ofMockMaker
this method should:- Create a proxy object that implements
settings.typeToMock
and potentially alsosettings.extraInterfaces
. - You may use the information from
settings
to create/configure your proxy object. - Your proxy object should carry the
handler
with it. For example, if you generate byte code to create the proxy you could generate an extra field to keep thehandler
with the generated object. Your implementation ofMockMaker
is required to provide this instance ofhandler
whengetHandler(Object)
is called.
- Type Parameters:
T
- Type of the mock to return, actually thesettings.getTypeToMock
.- Parameters:
settings
- - mock creation settings like type to mock, extra interfaces and so on.handler
- SeeMockHandler
. Do not provide your own implementation at this time. Make sure your implementation ofgetHandler(Object)
will return this instance.- Returns:
- The mock instance.
- Since:
- 1.9.5
- Create a proxy object that implements
-
getHandler
MockHandler getHandler(java.lang.Object mock)
Returns the handler for themock
. Do not provide your own implementations at this time because the work on theMockHandler
api is not completed. Use the instance provided to you by Mockito atcreateMock(org.mockito.mock.MockCreationSettings<T>, org.mockito.invocation.MockHandler)
orresetMock(java.lang.Object, org.mockito.invocation.MockHandler, org.mockito.mock.MockCreationSettings)
.- Parameters:
mock
- The mock instance.- Returns:
- may return null - it means that there is no handler attached to provided object. This means the passed object is not really a Mockito mock.
- Since:
- 1.9.5
-
resetMock
void resetMock(java.lang.Object mock, MockHandler newHandler, MockCreationSettings settings)
Replaces the existing handler onmock
withnewHandler
.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 passedmock
- Parameters:
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.- Since:
- 1.9.5
-
-