Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Android unit test Parcelable object.

Unit test parcel with android

Example

Model model = new Model("HelloWorld");
Model createdFromParcel = ParcelTestHelper.createFromParcel(model, model.CREATOR);

// Do your assertions...
assertEquals(model.value, createdFromParcel.value);

Features:

Create a copy of an object from a parcel

ParcelTestHelper.createFromParcel(model, yourCreator)

yourCreator is an optional parameter. It can be deduced with reflexion.

// @see field: ParcelTestHelper.DEFAULT_CREATOR_FIELD
ParcelTestHelper.createFromParcel(model);

Create a parcel from an object

ParcelTestHelper.toParcel(model)

Create an object from a parcel

ParcelTestHelper.fromParcel(model, yourCreator)

import android.os.Parcel;
import android.os.Parcelable;
public class ParcelTestHelper {
public static String DEFAULT_CREATOR_FIELD = "CREATOR";
public static <T extends Parcelable> T createFromParcel(T input, Parcelable.Creator<T> creator) {
Parcel parcel = toParcel(input);
return fromParcel(parcel, creator);
}
public static <T extends Parcelable> T createFromParcel(T input) throws NoSuchFieldException, IllegalAccessException {
return createFromParcel(input, getCreator(input));
}
public static <T extends Parcelable> Parcel toParcel(T input) {
Parcel parcel = Parcel.obtain();
input.writeToParcel(parcel, input.describeContents());
parcel.setDataPosition(0);
return parcel;
}
public static <T> Parcelable.Creator<T> getCreator(T input) throws NoSuchFieldException, IllegalAccessException {
return getCreator(input, DEFAULT_CREATOR_FIELD);
}
public static <T> Parcelable.Creator<T> getCreator(T input, String field) throws NoSuchFieldException, IllegalAccessException {
Object creator = input.getClass().getField(field).get(input);
if (!(creator instanceof Parcelable.Creator)) {
throw new InternalError("Should have field " + field + " instance of Parcelable.Creator");
}
return (Parcelable.Creator<T>) creator;
}
public static <T extends Parcelable> T fromParcel(Parcel parcel, Parcelable.Creator<T> creator) {
return creator.createFromParcel(parcel);
}
}
import android.os.Parcel;
import android.os.Parcelable;
import org.junit.Test;
import static org.junit.Assert.*;
public class ParcelTestHelperTest {
@Test
public void testWithCreator() {
Model model = new Model("HelloWorld");
Model createdFromParcel = ParcelTestHelper.createFromParcel(model, model.CREATOR);
assertEquals(model.value, createdFromParcel.value);
assertNotEquals("v2", createdFromParcel.value);
model.value = "newValue";
assertNotEquals(model.value, createdFromParcel.value);
}
@Test
public void testWithReflection() throws NoSuchFieldException, IllegalAccessException {
Model model = new Model("HelloWorld");
Model createdFromParcel = ParcelTestHelper.createFromParcel(model);
assertEquals(model.value, createdFromParcel.value);
assertNotEquals("v2", createdFromParcel.value);
model.value = "newValue";
assertNotEquals(model.value, createdFromParcel.value);
}
public static class Model implements Parcelable {
public String value;
public Model(Parcel in) {
value = in.readString();
}
public Model(String value) {
this.value = value;
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel parcel, int i) {
parcel.writeString(value);
}
public static final Creator<Model> CREATOR = new Creator<Model>() {
@Override
public Model createFromParcel(Parcel in) {
return new Model(in);
}
@Override
public Model[] newArray(int size) {
return new Model[size];
}
};
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment