Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
MockParcel for testing Parcelables implementations with the new Android's Unit testing support ( Only String and Long read/write implemented here. Add mock methods for other types.
import android.os.Parcel;
import java.util.ArrayList;
import java.util.List;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;
import static org.mockito.Matchers.anyInt;
import static org.mockito.Matchers.anyLong;
import static org.mockito.Matchers.anyString;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
public class MockParcel {
public static Parcel obtain() {
return new MockParcel().getMockedParcel();
Parcel mockedParcel;
int position;
List<Object> objects;
public Parcel getMockedParcel() {
return mockedParcel;
public MockParcel() {
mockedParcel = mock(Parcel.class);
objects = new ArrayList<>();
private void setupMock() {
private void setupWrites() {
Answer<Void> writeValueAnswer = new Answer<Void>() {
@Override public Void answer(InvocationOnMock invocation) throws Throwable {
Object parameter = invocation.getArguments()[0];
return null;
private void setupReads() {
when(mockedParcel.readLong()).thenAnswer(new Answer<Long>() {
@Override public Long answer(InvocationOnMock invocation) throws Throwable {
return (Long) objects.get(position++);
when(mockedParcel.readString()).thenAnswer(new Answer<String>() {
@Override public String answer(InvocationOnMock invocation) throws Throwable {
return (String) objects.get(position++);
private void setupOthers() {
doAnswer(new Answer<Void>() {
@Override public Void answer(InvocationOnMock invocation) throws Throwable {
position = ((Integer) invocation.getArguments()[0]);
return null;
Copy link

cdsap commented Feb 27, 2015


Copy link

daddykotex commented Mar 3, 2015


Copy link

kalvn commented Apr 13, 2015

Thanks for this. How would you do to test readStringArray ? I'm completely new to mockito and I kind of block on this one.

Copy link

jaredsburrows commented Jan 20, 2017

How is this working? Parcel is final right?

Copy link

jaymoid commented Jan 23, 2017

@jaredburrows, I was wondering the exact same thing! If you check in the stubbed android.jar it's still final, but if you inspect the Parcel class at runtime (in unit tests) it is no longer final, and therefore mockable:


Returns only "public"! 😮 So I guess Android do something to remove final modifiers so that you can mock them more easily.

Big thanks to Sloy for sharing.

Tip: if anyone has a lot of "reads" to stub, they can be made parametric to shorten the code a bit. eg:

private void setupReads() {
     // ... all your reads here ...

private <T> Answer<T> stubReadAnswer() {
    return invocation -> (T) objects.get(position++);

Copy link

hram commented Mar 29, 2018

this does not work if Parcelable class has nullable String field

Copy link

milosmns commented May 31, 2018

I made some edits to this approach (and also converted it to Kotlin) - you can check it out here:
It's a good approach overall, I've been able to test everything I needed using this.

Copy link

sienatime commented Aug 27, 2019

In Mockito 2, you can switch to the nullable matcher to make this match null strings as well:


anyString() used to also match null, but it doesn't anymore: mockito/mockito#185

Also works for nullable Parcelables.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment