1

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.

mbmc
  • 5,024
  • 5
  • 25
  • 53

1 Answers1

1

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.

mbmc
  • 5,024
  • 5
  • 25
  • 53
  • Here's the source of ParcelsTestUtil for reference: https://github.com/johncarl81/parceler/blob/master/examples/test/src/test/java/org/parceler/ParcelsTestUtil.java . You're correct, the key is to marshal and unmarshal the generated Parcelable through a android.os.Parcel. – John Ericksen Aug 31 '15 at 23:06