linkedin / linkedin/dexmaker

Support serialization of Mockito mocks

Open
#56 5 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

enhancement
Dominant language
Java
Stars
2k
Forks
262
PR merge metrics
No merged PRs in 30d

Description

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](https://gist.github.com/rsharifu/71a377e1c0c0ad293852c722df7cb729).
```
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](https://gist.github.com/rsharifu/db4948525075b0a1e85d2087d2cb0441)):
```
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?

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the Android emulator test from the issue using the shown Mockito serializable mock and inspect the com.android.dx.mockito.InvocationHandlerAdapter failure. Trace how the Android mock handler participates in ObjectOutputStream serialization. Done means the provided Entity mock can be serialized in an Android test without NotSerializableException.

Written by the indexing model from the issue text.

Assessment

Tech stack
android, java
Domain
mobile, testing
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.