Interface MockMaker
-
- All Known Subinterfaces:
InlineMockMaker
public interface MockMakerThe facility to create mocks.By default, an internal byte-buddy/asm/objenesis based implementation is used.
MockMakeris 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.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.AwesomeMockMakerthat 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.MockMakerfiles exists in the classpath Mockito will only use the first returned by the standardClassLoader.getResource(java.lang.String)mechanism.Using the MockSettings of individual mocks
If you want to use a
MockMakeronly for a specific mock, you can specify it usingMockSettings.mockMaker(String).// Use a built-in mock maker Object mock = Mockito.mock(Object.class, Mockito.withSettings() .mockMaker(MockMakers.INLINE)); // Or load a mock maker using a fully qualified class name Object mock = Mockito.mock(Object.class, Mockito.withSettings() .mockMaker("org.awesome.mockito.AwesomeMockMaker"));- Since:
- 1.9.5
- See Also:
MockCreationSettings,MockHandler
-
-
Nested Class Summary
Nested Classes Modifier and Type Interface Description static interfaceMockMaker.ConstructionMockControl<T>static interfaceMockMaker.StaticMockControl<T>static interfaceMockMaker.TypeMockabilityCarries the mockability information
-
Method Summary
All Methods Instance Methods Abstract Methods Default Methods Modifier and Type Method Description default voidclearAllCaches()Clears all cashes for mocked types and removes all byte code alterations, if possible.default <T> MockMaker.ConstructionMockControl<T>createConstructionMock(Class<T> type, Function<MockedConstruction.Context,MockCreationSettings<T>> settingsFactory, Function<MockedConstruction.Context,MockHandler<T>> handlerFactory, MockedConstruction.MockInitializer<T> mockInitializer)If you want to provide your own implementation ofMockMakerthis 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> TcreateMock(MockCreationSettings<T> settings, MockHandler handler)If you want to provide your own implementation ofMockMakerthis method should: Create a proxy object that implementssettings.typeToMockand potentially alsosettings.extraInterfaces. You may use the information fromsettingsto create/configure your proxy object. Your proxy object should carry thehandlerwith 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 ofMockMakerthis 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.MockHandlergetHandler(Object mock)Returns the handler for themock.MockMaker.TypeMockabilityisTypeMockable(Class<?> type)Indicates if the given type can be mocked by this mockmaker.voidresetMock(Object mock, MockHandler newHandler, MockCreationSettings settings)Replaces the existing handler onmockwithnewHandler.
-
-
-
Method Detail
-
createMock
<T> T createMock(MockCreationSettings<T> settings, MockHandler handler)
If you want to provide your own implementation ofMockMakerthis method should:- Create a proxy object that implements
settings.typeToMockand potentially alsosettings.extraInterfaces. - You may use the information from
settingsto create/configure your proxy object. - Your proxy object should carry the
handlerwith it. For example, if you generate byte code to create the proxy you could generate an extra field to keep thehandlerwith the generated object. Your implementation ofMockMakeris required to provide this instance ofhandlerwhengetHandler(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
-
createSpy
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. This avoids problems when creating spies of classes that declare effectively final instance fields where setting field values from outside the constructor is prohibited.- 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.instance- The object to spy upon.- Returns:
- The spy instance, if this mock maker supports direct spy creation.
- Since:
- 3.5.0
-
getHandler
MockHandler getHandler(Object mock)
Returns the handler for themock. Do not provide your own implementations at this time because the work on theMockHandlerapi 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:
- The mock handler, but 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(Object mock, MockHandler newHandler, MockCreationSettings settings)
Replaces the existing handler onmockwithnewHandler.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
newHandleris 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
-
isTypeMockable
MockMaker.TypeMockability isTypeMockable(Class<?> type)
Indicates if the given type can be mocked by this mockmaker.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.
- Parameters:
type- The type inspected for mockability.- Returns:
- object that carries the information about mockability of given type.
- Since:
- 2.1.0
-
createStaticMock
default <T> MockMaker.StaticMockControl<T> createStaticMock(Class<T> type, MockCreationSettings<T> settings, MockHandler handler)
If you want to provide your own implementation ofMockMakerthis 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.
- 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:
- A control for the static mock.
- Since:
- 3.4.0
-
createConstructionMock
default <T> MockMaker.ConstructionMockControl<T> createConstructionMock(Class<T> type, Function<MockedConstruction.Context,MockCreationSettings<T>> settingsFactory, Function<MockedConstruction.Context,MockHandler<T>> handlerFactory, MockedConstruction.MockInitializer<T> mockInitializer)
If you want to provide your own implementation ofMockMakerthis 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.
- Type Parameters:
T- Type of the mock to return, actually thesettings.getTypeToMock.- Parameters:
settingsFactory- Factory for mock creation settings like type to mock, extra interfaces and so on.handlerFactory- Factory for settings. SeeMockHandler. Do not provide your own implementation at this time. Make sure your implementation ofgetHandler(Object)will return this instance.- Returns:
- A control for the mocked construction.
- Since:
- 3.5.0
-
clearAllCaches
default void clearAllCaches()
Clears all cashes for mocked types and removes all byte code alterations, if possible.
-
-