Support serialization of Mockito mocks
See original GitHub issueI’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:
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:
- Created 7 years ago
- Comments:5 (3 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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.@drewhannay would it be a hack to make com.android.dx.mockito.InvocationHandlerAdapter Serializable?