Class ByteBuddyMockMaker
- java.lang.Object
-
- org.mockito.internal.creation.bytebuddy.ByteBuddyMockMaker
-
- All Implemented Interfaces:
ClassCreatingMockMaker,MockMaker
public class ByteBuddyMockMaker extends java.lang.Object implements ClassCreatingMockMaker
ByteBuddy MockMaker. This class will serve as the programmatic entry point to all mockito internal MockMakers. Currently the default and only mock maker is the subclassing engine, but with enough feedback we can later promote the inlining engine for features like final class/methods mocks. The programmatic API could look likemock(Final.class, withSettings().finalClasses()).
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from interface org.mockito.plugins.MockMaker
MockMaker.ConstructionMockControl<T>, MockMaker.StaticMockControl<T>, MockMaker.TypeMockability
-
-
Field Summary
Fields Modifier and Type Field Description private SubclassByteBuddyMockMakersubclassByteBuddyMockMaker
-
Constructor Summary
Constructors Constructor Description ByteBuddyMockMaker()ByteBuddyMockMaker(SubclassByteBuddyMockMaker subclassByteBuddyMockMaker)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidclearAllCaches()Clears all cashes for mocked types and removes all byte code alterations, if possible.<T> MockMaker.ConstructionMockControl<T>createConstructionMock(java.lang.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 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.<T> java.lang.Class<? extends T>createMockType(MockCreationSettings<T> creationSettings)<T> java.util.Optional<T>createSpy(MockCreationSettings<T> settings, MockHandler handler, T object)By implementing this method, a mock maker can optionally support the creation of spies where all fields are set within a constructor.<T> MockMaker.StaticMockControl<T>createStaticMock(java.lang.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(java.lang.Object mock)Returns the handler for themock.MockMaker.TypeMockabilityisTypeMockable(java.lang.Class<?> type)Indicates if the given type can be mocked by this mockmaker.voidresetMock(java.lang.Object mock, MockHandler newHandler, MockCreationSettings settings)Replaces the existing handler onmockwithnewHandler.
-
-
-
Field Detail
-
subclassByteBuddyMockMaker
private final SubclassByteBuddyMockMaker subclassByteBuddyMockMaker
-
-
Constructor Detail
-
ByteBuddyMockMaker
public ByteBuddyMockMaker()
-
ByteBuddyMockMaker
ByteBuddyMockMaker(SubclassByteBuddyMockMaker subclassByteBuddyMockMaker)
-
-
Method Detail
-
createMock
public <T> T createMock(MockCreationSettings<T> settings, MockHandler handler)
Description copied from interface:MockMakerIf 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 ofhandlerwhenMockMaker.getHandler(Object)is called.
- Specified by:
createMockin interfaceMockMaker- 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 ofMockMaker.getHandler(Object)will return this instance.- Returns:
- The mock instance.
- Create a proxy object that implements
-
createSpy
public <T> java.util.Optional<T> createSpy(MockCreationSettings<T> settings, MockHandler handler, T object)
Description copied from interface:MockMakerBy 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.- Specified by:
createSpyin interfaceMockMaker- 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 ofMockMaker.getHandler(Object)will return this instance.object- The object to spy upon.- Returns:
- The spy instance, if this mock maker supports direct spy creation.
-
createMockType
public <T> java.lang.Class<? extends T> createMockType(MockCreationSettings<T> creationSettings)
- Specified by:
createMockTypein interfaceClassCreatingMockMaker
-
getHandler
public MockHandler getHandler(java.lang.Object mock)
Description copied from interface:MockMakerReturns 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 atMockMaker.createMock(org.mockito.mock.MockCreationSettings<T>, org.mockito.invocation.MockHandler)orMockMaker.resetMock(java.lang.Object, org.mockito.invocation.MockHandler, org.mockito.mock.MockCreationSettings).- Specified by:
getHandlerin interfaceMockMaker- 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.
-
resetMock
public void resetMock(java.lang.Object mock, MockHandler newHandler, MockCreationSettings settings)Description copied from interface:MockMakerReplaces 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
-
isTypeMockable
@Incubating public MockMaker.TypeMockability isTypeMockable(java.lang.Class<?> type)
Description copied from interface:MockMakerIndicates 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.
- Specified by:
isTypeMockablein interfaceMockMaker- Parameters:
type- The type inspected for mockability.- Returns:
- object that carries the information about mockability of given type.
-
createStaticMock
public <T> MockMaker.StaticMockControl<T> createStaticMock(java.lang.Class<T> type, MockCreationSettings<T> settings, MockHandler handler)
Description copied from interface:MockMakerIf 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.
- Specified by:
createStaticMockin interfaceMockMaker- Type Parameters:
T- Type of the mock to return, actually thesettings.getTypeToMock.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 ofMockMaker.getHandler(Object)will return this instance.- Returns:
- A control for the static mock.
-
createConstructionMock
public <T> MockMaker.ConstructionMockControl<T> createConstructionMock(java.lang.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)
Description copied from interface:MockMakerIf 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.
- Specified by:
createConstructionMockin interfaceMockMaker- Type Parameters:
T- Type of the mock to return, actually thesettings.getTypeToMock.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 ofMockMaker.getHandler(Object)will return this instance.- Returns:
- A control for the mocked construction.
-
clearAllCaches
public void clearAllCaches()
Description copied from interface:MockMakerClears all cashes for mocked types and removes all byte code alterations, if possible.- Specified by:
clearAllCachesin interfaceMockMaker
-
-