samedi 2 juillet 2016

Android Testing - Mocked Parcel returns empty objects

I am Unit testing my android app and I have a class that implements Parcelable. For some reason, when I write a test to check my implementation, the mocked Parcel does not deserialize my object correctly.

This is how I am testing the parcelable implementation:

@Test
public void parcelableWriteReadWorks() {
    Parcel parcel = mock(Parcel.class);
    mRightMassage.writeToParcel(parcel, 0);
    parcel.setDataPosition(0);
    Massage newMassage = Massage.makeMassage(parcel);
    assertEquals(mRightMassage, newMassage);
}

The assertEquals is throwing a NullPointerException because all the fields in the "newMassage" object are empty.

Can you help me to figure out what is going on?

This is my implementation of the parcelable interface for the class:

    /** Required {@code Creator} implementation for the {@link Parcelable} interface */
public static final Creator<Massage> CREATOR = new Creator<Massage>() {
    @Contract("_ -> !null")
    @Override
    public Massage createFromParcel(Parcel in) {
        return new Massage(in);
    }

    @Contract(value = "_ -> !null", pure = true)
    @Override
    public Massage[] newArray(int size) {
        return new Massage[size];
    }
};


/**
 * Constructor created to implement the {@link Parcelable} interface. Reads data from a parcel
 * and includes it as the member fields.
 */
protected Massage(Parcel in) {
    bodyParts = in.readString();
    description = in.readString();
    duration = in.readString();
    headline = in.readString();
    intensity = in.readInt();
    name = in.readString();
    posterPath = in.readString();
    price = in.readInt();
}

/**
 * Describe the kinds of special objects contained in this Parcelable's
 * marshalled representation.
 *
 * @return a bitmask indicating the set of special object types marshalled
 * by the Parcelable.
 */
@Contract(pure = true)
@Override
public int describeContents() {
    return 0;
}

/**
 * Flatten this object in to a Parcel.
 *
 * @param dest  The Parcel in which the object should be written.
 * @param flags Additional flags about how the object should be written.
 *              May be 0 or {@link #PARCELABLE_WRITE_RETURN_VALUE}.
 */
@Override
public void writeToParcel(Parcel dest, int flags) {
    dest.writeString(bodyParts);
    dest.writeString(description);
    dest.writeString(duration);
    dest.writeString(headline);
    dest.writeInt(intensity);
    dest.writeString(name);
    dest.writeString(posterPath);
    dest.writeInt(price);
}

@Override
public boolean equals(Object o) {
    if (this == o) return true;
    if (o == null || getClass() != o.getClass()) return false;

    Massage massage = (Massage) o;

    return intensity == massage.intensity && name.equals(massage.name)
            && bodyParts.equals(massage.bodyParts);
}

@Override
public int hashCode() {
    int result = name.hashCode();
    result = 31 * result + intensity;
    result = 31 * result + bodyParts.hashCode();
    return result;
}
}

Aucun commentaire:

Enregistrer un commentaire