Mockito: spy() serializable support

Created on 8 Aug 2016  路  5Comments  路  Source: mockito/mockito

check that

  • [x] The mockito message in the stacktrace have useful information, but it didn't help
  • [x] The problematic code (if that's possible) is copied here;
    Note that some configuration are impossible to mock via Mockito
  • [ ] Provide versions (mockito / jdk / os / any other relevant information)
  • [ ] Provide a Short, Self Contained, Correct (Compilable), Example of the issue
    (same as any question on stackoverflow.com)
  • [ ] Read the contributing guide

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())
enhancement

Most helpful comment

mock(class,
withSettings()
.serializable()
.spiedInstance(object)
.defaultAnswer(CALLS_REAL_METHODS));

All 5 comments

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.

Was this page helpful?
0 / 5 - 0 ratings