Search code examples
androidparceler

How to test @parcel objects as if they were sent across activities


When I unit test @parcel annotated models, I get the same object (even when using Bundle, or Intent):

@Test public void parcel() {
    MyObject myObject = new MyObject("123");
    Parcelable parcelable = Parcels.wrap(myObject);
    MyObject in = Parcels.unwrap(parcelable);
    // myObject == in (same id)
}

How to simulate the situation where the model is passed from one activity to the other, hence being recreated?

update: might need to use this.


Solution

  • Using this file:

    @Test public void parcel() {
        MyObject myObject = new MyObject("123");
        MyObject in = Parcels.unwrap(ParcelsTestUtil.wrap(myObject);
        assertNotEquals(myObject.hasCode(), in.hasCode())
    }
    

    The test needs to be run with an Android test or Robolectric artifact.