Class InlineDelegateByteBuddyMockMaker
- java.lang.Object
-
- org.mockito.internal.creation.bytebuddy.InlineDelegateByteBuddyMockMaker
-
- All Implemented Interfaces:
Instantiator
,ClassCreatingMockMaker
,InlineMockMaker
,MockMaker
@Incubating @SuppressSignatureCheck class InlineDelegateByteBuddyMockMaker extends java.lang.Object implements ClassCreatingMockMaker, InlineMockMaker, Instantiator
Agent and subclass based mock maker.This mock maker which uses a combination of the Java instrumentation API and sub-classing rather than creating a new sub-class to create a mock. This way, it becomes possible to mock final types and methods. This mock maker must to be activated explicitly for supporting mocking final types and methods:
This mock maker can be activated by creating the file
/mockito-extensions/org.mockito.plugins.MockMaker
containing the textmock-maker-inline
ororg.mockito.internal.creation.bytebuddy.InlineByteBuddyMockMaker
.This mock maker will make a best effort to avoid subclass creation when creating a mock. Otherwise it will use the
org.mockito.internal.creation.bytebuddy.SubclassByteBuddyMockMaker
to create the mock class. That means that the following condition is trueclass Foo { } assert mock(Foo.class).getClass() == Foo.class;
unless any of the following conditions is met, in such case the mock maker fall backs to the the creation of a subclass.
- the type to mock is an abstract class.
- the mock is set to require additional interfaces.
- the mock is explicitly set to support serialization.
Some type of the JDK cannot be mocked, this includes
Class
,String
, and wrapper types.Nevertheless, final methods of such types are mocked when using the inlining mock maker. Mocking final types and enums does however remain impossible when explicitly requiring serialization support or when adding ancillary interfaces.
Important behavioral changes when using inline-mocks:
- Mockito is capable of mocking package-private methods even if they are defined in different packages than
the mocked type. Mockito voluntarily never mocks package-visible methods within
java.*
packages. - Additionally to final types, Mockito can now mock types that are not visible for extension; such types include private types in a protected package.
- Mockito can no longer mock
native
methods. Inline mocks require byte code manipulation of a method where native methods do not offer any byte code to manipulate. - Mockito cannot longer strip
synchronized
modifiers from mocked instances.
Note that inline mocks require a Java agent to be attached. Mockito will attempt an attachment of a Java agent upon loading the mock maker for creating inline mocks. Such runtime attachment is only possible when using a JVM that is part of a JDK or when using a Java 9 VM. When running on a non-JDK VM prior to Java 9, it is however possible to manually add the Byte Buddy Java agent jar using the
-javaagent
parameter upon starting the JVM. Furthermore, the inlining mock maker requires the VM to support class retransformation (also known as HotSwap). All major VM distributions such as HotSpot (OpenJDK), J9 (IBM/Websphere) or Zing (Azul) support this feature.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private static class
InlineDelegateByteBuddyMockMaker.InlineConstructionMockContext
private class
InlineDelegateByteBuddyMockMaker.InlineConstructionMockControl<T>
private static class
InlineDelegateByteBuddyMockMaker.InlineStaticMockControl<T>
-
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 BytecodeGenerator
bytecodeGenerator
private java.lang.ThreadLocal<java.lang.Object>
currentSpied
private static java.lang.Throwable
INITIALIZATION_ERROR
private static java.lang.instrument.Instrumentation
INSTRUMENTATION
private DetachedThreadLocal<java.util.Map<java.lang.Class<?>,java.util.function.BiConsumer<java.lang.Object,MockedConstruction.Context>>>
mockedConstruction
private DetachedThreadLocal<java.util.Map<java.lang.Class<?>,MockMethodInterceptor>>
mockedStatics
private java.lang.ThreadLocal<java.lang.Boolean>
mockitoConstruction
private WeakConcurrentMap<java.lang.Object,MockMethodInterceptor>
mocks
-
Constructor Summary
Constructors Constructor Description InlineDelegateByteBuddyMockMaker()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
clearAllCaches()
Clears all cashes for mocked types and removes all byte code alterations, if possible.void
clearAllMocks()
Cleans up internal state for all existing mocks.void
clearMock(java.lang.Object mock)
Clean up internal state for specifiedmock
.<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 ofMockMaker
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 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.<T> java.lang.Class<? extends T>
createMockType(MockCreationSettings<T> settings)
<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 ofMockMaker
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.private <T> T
doCreateMock(MockCreationSettings<T> settings, MockHandler handler, boolean nullOnNonInlineConstruction)
MockHandler
getHandler(java.lang.Object mock)
Returns the handler for themock
.MockMaker.TypeMockability
isTypeMockable(java.lang.Class<?> type)
Indicates if the given type can be mocked by this mockmaker.private java.lang.Object
makeStandardArgument(java.lang.Class<?> type)
<T> T
newInstance(java.lang.Class<T> cls)
Creates instance of given classprivate <T> java.lang.RuntimeException
prettifyFailure(MockCreationSettings<T> mockFeatures, java.lang.Exception generationFailed)
void
resetMock(java.lang.Object mock, MockHandler newHandler, MockCreationSettings settings)
Replaces the existing handler onmock
withnewHandler
.
-
-
-
Field Detail
-
INSTRUMENTATION
private static final java.lang.instrument.Instrumentation INSTRUMENTATION
-
INITIALIZATION_ERROR
private static final java.lang.Throwable INITIALIZATION_ERROR
-
bytecodeGenerator
private final BytecodeGenerator bytecodeGenerator
-
mocks
private final WeakConcurrentMap<java.lang.Object,MockMethodInterceptor> mocks
-
mockedStatics
private final DetachedThreadLocal<java.util.Map<java.lang.Class<?>,MockMethodInterceptor>> mockedStatics
-
mockedConstruction
private final DetachedThreadLocal<java.util.Map<java.lang.Class<?>,java.util.function.BiConsumer<java.lang.Object,MockedConstruction.Context>>> mockedConstruction
-
mockitoConstruction
private final java.lang.ThreadLocal<java.lang.Boolean> mockitoConstruction
-
currentSpied
private final java.lang.ThreadLocal<java.lang.Object> currentSpied
-
-
Method Detail
-
createMock
public <T> T createMock(MockCreationSettings<T> settings, MockHandler handler)
Description copied from interface:MockMaker
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
whenMockMaker.getHandler(Object)
is called.
- Specified by:
createMock
in 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:MockMaker
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.- Specified by:
createSpy
in 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.
-
doCreateMock
private <T> T doCreateMock(MockCreationSettings<T> settings, MockHandler handler, boolean nullOnNonInlineConstruction)
-
createMockType
public <T> java.lang.Class<? extends T> createMockType(MockCreationSettings<T> settings)
- Specified by:
createMockType
in interfaceClassCreatingMockMaker
-
prettifyFailure
private <T> java.lang.RuntimeException prettifyFailure(MockCreationSettings<T> mockFeatures, java.lang.Exception generationFailed)
-
getHandler
public MockHandler getHandler(java.lang.Object mock)
Description copied from interface:MockMaker
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 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:
getHandler
in 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:MockMaker
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
-
clearAllCaches
public void clearAllCaches()
Description copied from interface:MockMaker
Clears all cashes for mocked types and removes all byte code alterations, if possible.- Specified by:
clearAllCaches
in interfaceMockMaker
-
clearMock
public void clearMock(java.lang.Object mock)
Description copied from interface:InlineMockMaker
Clean up internal state for specifiedmock
. You may assume there won't be any interaction to the specific mock after this is called.- Specified by:
clearMock
in interfaceInlineMockMaker
- Parameters:
mock
- the mock instance whose internal state is to be cleaned.
-
clearAllMocks
public void clearAllMocks()
Description copied from interface:InlineMockMaker
Cleans up internal state for all existing mocks. You may assume there won't be any interaction to mocks created previously after this is called.- Specified by:
clearAllMocks
in interfaceInlineMockMaker
-
isTypeMockable
public MockMaker.TypeMockability isTypeMockable(java.lang.Class<?> type)
Description copied from interface:MockMaker
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.
- Specified by:
isTypeMockable
in 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:MockMaker
If you want to provide your own implementation ofMockMaker
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.
- Specified by:
createStaticMock
in 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:MockMaker
If you want to provide your own implementation ofMockMaker
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.
- Specified by:
createConstructionMock
in 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.
-
newInstance
public <T> T newInstance(java.lang.Class<T> cls) throws InstantiationException
Description copied from interface:Instantiator
Creates instance of given class- Specified by:
newInstance
in interfaceInstantiator
- Throws:
InstantiationException
-
makeStandardArgument
private java.lang.Object makeStandardArgument(java.lang.Class<?> type)
-
-