question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Support serialization of Mockito mocks

See original GitHub issue

I’m writing a unit-test for Android app, which creates a mock of serializable custom class and tries to serialize it. It works fine in JUnit test when I’m running it in JVM, but fails with following error when I’m running it as Android test in emulator:

Full gist of error message.

W/System.err: java.io.NotSerializableException: com.android.dx.mockito.InvocationHandlerAdapter
W/System.err:     at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1224)
W/System.err:     at java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1584)
W/System.err:     at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1549)
W/System.err:     at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1472)

Here is my code (Android test gist):

Entity serializableMock = mock(Entity.class, Mockito.withSettings().serializable());
try {
    ObjectOutputStream out = new ObjectOutputStream(new ByteArrayOutputStream());
    out.writeObject(serializableMock);
} catch (IOException e) {//catching NotSerializableException here} 

Entity:

package org.test;
public class Entity implements Serializable {
    public String str;
    // getters/setters
}

Gradle dependencies:

androidTestCompile 'org.mockito:mockito-core:2.4.0'
androidTestCompile 'com.linkedin.dexmaker:dexmaker-mockito:2.2.0'
androidTestCompile "com.android.support:support-annotations:25.0.0"
androidTestCompile 'com.android.support.test:runner:0.5'
androidTestCompile 'com.android.support.test:rules:0.5'

How to properly serialize mock object on Android?

Issue Analytics

  • State:open
  • Created 7 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
drewhannaycommented, Mar 8, 2017

That was my first try. 😉

I don’t think that will work though…the actual code needed to make serializable mocks behave as expected is pretty complex. It may be worth checking out the new mockito-android project from the mockito team if this is important to you.

0reactions
engintekincommented, Mar 8, 2017

@drewhannay would it be a hack to make com.android.dx.mockito.InvocationHandlerAdapter Serializable?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Serializing a mock throws exception - java - Stack Overflow
According to the Mockito documentation, you can make a mock serializable: List serializableMock = mock(List.class, withSettings().serializable());.
Read more >
Mockito mocks can be serialized / deserialized ... - CWIKI.US
Mockito introduces serialization across classloader. Like with any other form of serialization, all types in the mock hierarchy have to ...
Read more >
MockSettings (Mockito 3.2.4 API) - Javadoc.io
Configures the mock to be serializable with a specific serializable mode. With this feature you can use a mock in a place that...
Read more >
Serializable flag doesn't make mock serializable #1618 - GitHub
I have a simple example test like this (I'm using junit5 with mockito-junit-jupiter extension) @ExtendWith(MockitoExtension.class) class ...
Read more >
Issues serializing mock objects into Json - Google Groups
Mockito only supports standard serialization of the JDK. Also remember the principle don't mock values! ;) It feels wrong to serialize mocked values,...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found