android-test icon indicating copy to clipboard operation
android-test copied to clipboard

android.os.BadParcelableException: ClassNotFoundException when unmarshalling

Open plastiv opened this issue 5 years ago • 2 comments

Description

Asserting on activity result extra from ActivityScenario results in exception. This was not the case with ActivityTestRule.

Steps to Reproduce

  1. Start "picker" activity with scenario = ActivityScenario.launch(intent)
  2. Simulate result_ok with included parcelable extra
  3. Assert on activity result intent extras (which triggers unmarshalling ClassNotFoundException exception) with
val activityResult = scenario.result!!
assertThat(activityResult, hasResultCode(RESULT_OK))
assertThat(activityResult, hasResultData(hasExtraWithKey(KEY_SELECTED_CITY)))

Expected Results

Assertion works

Actual Results

Exception similar to next one:

android.os.BadParcelableException: ClassNotFoundException when unmarshalling: my.package.ParcelableAutoCompleteCity
at android.os.Parcel.readParcelableCreator(Parcel.java:3042)
at android.os.Parcel.readParcelable(Parcel.java:2964)
at android.os.Parcel.readValue(Parcel.java:2866)
at android.os.Parcel.readArrayMapInternal(Parcel.java:3244)
at android.os.BaseBundle.initializeFromParcelLocked(BaseBundle.java:292)
at android.os.BaseBundle.unparcel(BaseBundle.java:236)
at android.os.BaseBundle.keySet(BaseBundle.java:562)
at androidx.test.espresso.intent.matcher.BundleMatchers$BundleMatcher.matchesSafely(BundleMatchers.java:47)
at androidx.test.espresso.intent.matcher.BundleMatchers$BundleMatcher.matchesSafely(BundleMatchers.java:35)
at org.hamcrest.TypeSafeMatcher.matches(TypeSafeMatcher.java:67)
at androidx.test.espresso.intent.matcher.IntentMatchers$7.matchesSafely(IntentMatchers.java:215)
at androidx.test.espresso.intent.matcher.IntentMatchers$7.matchesSafely(IntentMatchers.java:206)
at org.hamcrest.TypeSafeMatcher.matches(TypeSafeMatcher.java:67)
at androidx.test.espresso.contrib.ActivityResultMatchers$1.matchesSafely(ActivityResultMatchers.java:46)
at androidx.test.espresso.contrib.ActivityResultMatchers$1.matchesSafely(ActivityResultMatchers.java:37)
at org.hamcrest.TypeSafeMatcher.matches(TypeSafeMatcher.java:67)
at androidx.test.espresso.matcher.ViewMatchers.assertThat(ViewMatchers.java:7)
at androidx.test.espresso.matcher.ViewMatchers.assertThat(ViewMatchers.java:5)

AndroidX Test and Android OS Versions

1.3.0 and sdk29

Link to a public git repo demonstrating the problem:

I need some guidance. I guess this repo should have already some tests which work on activity result verification. What are they? I would then extend them to include parcelable extra into activity result intent.

Current workaround

Set classloader explicitly before asserting result

val activityResult = scenario.result!!
activityResult.resultData.setExtrasClassLoader(this::class.java.classLoader)
assertThat(activityResult, hasResultCode(RESULT_OK))
assertThat(activityResult, hasResultData(hasExtraWithKey(KEY_SELECTED_CITY)))

plastiv avatar Sep 07 '20 13:09 plastiv

Yes, it is reproducible. Are there any deadlines for when it will be fixed?

nick2525 avatar Oct 14 '21 14:10 nick2525

Can this be handled before the call?

Chrn0924 avatar Jul 17 '23 03:07 Chrn0924