Class SubclassByteBuddyMockMaker
- java.lang.Object
-
- org.mockito.internal.creation.bytebuddy.SubclassByteBuddyMockMaker
-
- All Implemented Interfaces:
ClassCreatingMockMaker
,MockMaker
public class SubclassByteBuddyMockMaker extends java.lang.Object implements ClassCreatingMockMaker
Subclass based mock maker. This mock maker tries to create a subclass to represent a mock. It uses the given mock settings, that contains the type to mock, extra interfaces, and serialization support.The type to mock has to be not final and not part of the JDK. THe created mock will implement extra interfaces if any. And will implement
Serializable
if this settings is explicitly set.
-
-
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 BytecodeGenerator
cachingMockBytecodeGenerator
-
Constructor Summary
Constructors Constructor Description SubclassByteBuddyMockMaker()
SubclassByteBuddyMockMaker(SubclassLoader loader)
-
Method Summary
All Methods Static 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.<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)
private static java.lang.String
describeClass(java.lang.Class<?> type)
private static java.lang.String
describeClass(java.lang.Object instance)
private static <T> T
ensureMockIsAssignableToMockedType(MockCreationSettings<T> settings, T mock)
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 <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
.-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface org.mockito.plugins.MockMaker
createConstructionMock, createSpy, createStaticMock
-
-
-
-
Field Detail
-
cachingMockBytecodeGenerator
private final BytecodeGenerator cachingMockBytecodeGenerator
-
-
Constructor Detail
-
SubclassByteBuddyMockMaker
public SubclassByteBuddyMockMaker()
-
SubclassByteBuddyMockMaker
public SubclassByteBuddyMockMaker(SubclassLoader loader)
-
-
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
-
createMockType
public <T> java.lang.Class<? extends T> createMockType(MockCreationSettings<T> settings)
- Specified by:
createMockType
in interfaceClassCreatingMockMaker
-
ensureMockIsAssignableToMockedType
private static <T> T ensureMockIsAssignableToMockedType(MockCreationSettings<T> settings, T mock)
-
prettifyFailure
private <T> java.lang.RuntimeException prettifyFailure(MockCreationSettings<T> mockFeatures, java.lang.Exception generationFailed)
-
describeClass
private static java.lang.String describeClass(java.lang.Class<?> type)
-
describeClass
private static java.lang.String describeClass(java.lang.Object instance)
-
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
-
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.
-
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
-
-