holdenk / spark-testing-base

Base classes to use when writing tests with Spark
Apache License 2.0
1.51k stars 359 forks source link

Serialization for Mockito improvement #33

Open RadoBuransky opened 8 years ago

RadoBuransky commented 8 years ago

One single line can save some serialization pain when using Mockito. Perhaps it makes sense to create a mixin for StreamingSuiteCommon with MockitoSugar and this:

override def mock[T <: AnyRef](implicit manifest: Manifest[T]): T = super.mock[T](withSettings().serializable())

Otherwise you get following error:

Caused by: java.io.NotSerializableException: org.mockito.internal.creation.DelegatingMethod
Serialization stack:
    - object not serializable (class: org.mockito.internal.creation.DelegatingMethod, value: org.mockito.internal.creation.DelegatingMethod@a97f2bff)
    - field (class: org.mockito.internal.invocation.InvocationImpl, name: method, type: interface org.mockito.internal.invocation.MockitoMethod)
...
afedulov commented 8 years ago

@RadoBuransky this one-liner saved my day, thanks!

deepakmundhada commented 8 years ago

Thanks a ton! It worked for me.

nilsmagnus commented 7 years ago

For the javaheads, it is very similar:

SomeService mockedService = Mockito.mock(SomeService.class, withSettings().serializable());

samudurand commented 7 years ago

Man that's just so miraculous !! I never considered mockito could do that and I was even creating my own inherited mocks...

rugobal commented 7 years ago

Hi, creating a trait that overrides that method fixed the serialization issue. But now I get a different one:

java.lang.ClassNotFoundException: com.my.package.MyMockedObject$MockitoMock$2133292710

Anybody had the same problem?

pradeepcheers commented 6 years ago

I had the same issue as @rugobal mentioned. I had to stub the mock object by declaring it as serializable. The following links Not Serializable and Class Cast Exception have some useful information about these issues.

bowofolaf commented 6 years ago

You saved my life.