android / android/android-test
android.os.BadParcelableException: ClassNotFoundException when unmarshalling
- Dominant language
- Java
- Stars
- 1.2k
- Forks
- 342
- Avg merge
- 11h 29m
- Merged PRs (30d)
- 2
Description
### Description
Asserting on activity result extra from `ActivityScenario` results in exception. This was not the case with `ActivityTestRule`.
### Steps to Reproduce
2. Start "picker" activity with `scenario = ActivityScenario.launch(intent)`
3. Simulate result_ok with included parcelable extra
4. 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)))
```
Contributor guide
Research direction
Start with ActivityScenario.result and the ActivityResultMatchers and BundleMatchers paths shown in the stack trace. Reproduce the parcelable-extra assertion on SDK 29, then inspect existing activity-result verification tests and extend one to cover unmarshalling. Done means the assertion succeeds without manually setting the extras class loader.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java, kotlin
- Domain
- mobile-dev, testing
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100