Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
How to test a parcelable in androidTest
public class ParcelableTest extends AndroidTestCase {
* Parcels the given Parcelable, unparcels it and returns the unparceled value
* @param value the Parcelable to operate on
public static Parcelable parcelAndUnparcel(Parcelable value) {
Parcel parcel = Parcel.obtain();
value.writeToParcel(parcel, 0);
try {
Field creatorField = value.getClass().getField("CREATOR");
Parcelable.Creator creator = (Parcelable.Creator) creatorField.get(value);
return (Parcelable) creator.createFromParcel(parcel);
} catch (Exception e) {
return null;
} finally {
public void testParceling() {
final MyParcel p = new MyParcel();
assertEquals(p, parcelAndUnparcel(p));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment