check that
This is an issue for the latest version. Support serializable() for the mock() is a great feature!
According to the source code. spy also uses mock()
Why don't you support mockSetting argument for spy() like below code snippet? Is there any problem when it comes to call .serializable() with .spiedInstance()
public static <T> T spy(T object, MockSettings mockSettings) {
return MOCKITO_CORE.mock((Class<T>) object.getClass(), mockSettings.withSettings()
.spiedInstance(object)
.defaultAnswer(CALLS_REAL_METHODS));
}
val spyMyObject = spy(myObject, witSettings.serializable())
Interesting idea.
Its a much needed feature for classes using spark and other related frameworks.
mock(class,
withSettings()
.serializable()
.spiedInstance(object)
.defaultAnswer(CALLS_REAL_METHODS));
I have faced the same situation.
We need serializable spy!
Dear @bric3,
I think that if the subject of spy or mock method implements Serializable, then Mockito should return serializable instance with no exceptions and current behavior should be treated as a bug!
While this:
mock(class,
withSettings()
.serializable()
.spiedInstance(object)
.defaultAnswer(CALLS_REAL_METHODS));
indeed works, it should be treated only as a workaround until the framework fixes it's behavior.
Most helpful comment
mock(class,
withSettings()
.serializable()
.spiedInstance(object)
.defaultAnswer(CALLS_REAL_METHODS));