@Incubating public class AcrossJVMSerializationFeature extends java.lang.Object implements java.io.Serializable
Serializable
.
The way it works is to enable serialization via the enableSerializationAcrossJVM(MockCreationSettings)
,
if the mock settings is set to be serializable it will add the AcrossJVMSerializationFeature.AcrossJVMMockitoMockSerializable
interface.
This interface defines a the AcrossJVMSerializationFeature.AcrossJVMMockitoMockSerializable.writeReplace()
whose signature match the one that is looked by the standard Java serialization.
Then in the MethodInterceptorFilter
of mockito, if the writeReplace
method is called,
it will use the custom implementation of this class writeReplace(Object)
. This method has a specific
knowledge on how to serialize a mockito mock that is based on CGLIB.
Only one instance per mock! See MethodInterceptorFilter
CglibMockMaker
,
MethodInterceptorFilter
,
Serialized FormModifier and Type | Class and Description |
---|---|
static interface |
AcrossJVMSerializationFeature.AcrossJVMMockitoMockSerializable
Simple interface that hold a correct
writeReplace signature that can be seen by an
ObjectOutputStream . |
static class |
AcrossJVMSerializationFeature.AcrossJVMMockSerializationProxy
This is the serialization proxy that will encapsulate the real mock data as a byte array.
|
static class |
AcrossJVMSerializationFeature.MockitoMockObjectInputStream
Special Mockito aware
ObjectInputStream that will resolve the Mockito proxy class. |
Constructor and Description |
---|
AcrossJVMSerializationFeature() |
Modifier and Type | Method and Description |
---|---|
<T> void |
enableSerializationAcrossJVM(MockCreationSettings<T> settings)
Enable serialization serialization that will work across classloaders / and JVM.
|
boolean |
isWriteReplace(java.lang.reflect.Method method) |
java.lang.Object |
writeReplace(java.lang.Object mockitoMock)
Custom implementation of the
writeReplace method for serialization. |
public boolean isWriteReplace(java.lang.reflect.Method method)
public java.lang.Object writeReplace(java.lang.Object mockitoMock) throws java.io.ObjectStreamException
writeReplace
method for serialization.
Here's how it's working and why :
When first entering in this method, it's because some is serializing the mock, with some code like :
objectOutputStream.writeObject(mock);
So, ObjectOutputStream
will track the writeReplace
method in the instance and
execute it, which is wanted to replace the mock by another type that will encapsulate the actual mock.
At this point, the code will return an
AcrossJVMSerializationFeature.AcrossJVMMockSerializationProxy
.
Now, in the constructor
org.mockito.internal.creation.AcrossJVMSerializationFeature.AcrossJVMMockSerializationProxy#AcrossJVMMockSerializationProxy(Object)
the mock is being serialized in a custom way (using
AcrossJVMSerializationFeature.MockitoMockObjectOutputStream
) to a
byte array. So basically it means the code is performing double nested serialization of the passed
mockitoMock
.
However the ObjectOutputStream
will still detect the custom
writeReplace
and execute it.
(For that matter disabling replacement via ObjectOutputStream.enableReplaceObject(boolean)
doesn't disable the writeReplace
call, but just just toggle replacement in the
written stream, writeReplace
is always called by
ObjectOutputStream
.)
In order to avoid this recursion, obviously leading to a StackOverflowError
, this method is using
a flag that marks the mock as already being replaced, and then shouldn't replace itself again.
This flag is local to this class, which means the flag of this class unfortunately needs
to be protected against concurrent access, hence the reentrant lock.
mockitoMock
- The Mockito mock to be serialized.AcrossJVMSerializationFeature.AcrossJVMMockSerializationProxy
) to be serialized by the calling ObjectOutputStream.java.io.ObjectStreamException
public <T> void enableSerializationAcrossJVM(MockCreationSettings<T> settings)
Only enable if settings says the mock should be serializable. In this case add the
AcrossJVMSerializationFeature.AcrossJVMMockitoMockSerializable
to the extra interface list.
T
- Type param to not be bothered by the genericssettings
- Mock creation settings.