Repository navigation
Closed
Description
We have an Android project using Mockito and we tried to upgrade from 5.0.0 to 5.1.0.
When running the Android tests we always receive an error like this:
java.lang.NoClassDefFoundError: Failed resolution of: Ljava/lang/StackWalker$Option;
at org.mockito.internal.debugging.LocationImpl.stackWalker(LocationImpl.java:138)
at org.mockito.internal.debugging.LocationImpl.<clinit>(LocationImpl.java:40)
at org.mockito.internal.debugging.LocationFactory.create(LocationFactory.java:17)
at org.mockito.internal.debugging.LocationFactory.create(LocationFactory.java:13)
at org.mockito.internal.matchers.LocalizedMatcher.<init>(LocalizedMatcher.java:19)
at org.mockito.internal.progress.ArgumentMatcherStorageImpl.reportMatcher(ArgumentMatcherStorageImpl.java:30)
at org.mockito.ArgumentMatchers.reportMatcher(ArgumentMatchers.java:1036)
at org.mockito.ArgumentMatchers.any(ArgumentMatchers.java:177)
...
After some investigation we found that:
- StackWalker is only available from Java 9. All our modules are setup to use Java 11.
- We can't see StackWalker class from the IDE
- This only happens on Android instrumental tests, not in "normal" java tests
- The class StackWalker is not available in Android SDK java implementation: https://github.com/AndroidSDKSources/android-sdk-sources-for-api-level-33/tree/master/java/lang (that would explain why is not working only for instrumental tests, as they are executed on an Android device). Checked for latest android versions.
It looks like a very specific Android issue.
For the meantime, we will halt the upgrade, but any help will be great.
Thanks in advance
- The mockito message in the stacktrace have useful information, but it didn't help
- [N/A] The problematic code (if that's possible) is copied here;
Note that some configuration are impossible to mock via Mockito - Provide versions (mockito / jdk / os / any other relevant information)
- Provide a Short, Self Contained, Correct (Compilable), Example of the issue
(same as any question on stackoverflow.com) - Read the contributing guide
Reacted by Zsolt Világos, Róbert Papp and Alex Zhukov
Metadata
Metadata
Assignees
Labels
No labels
Activity
TimvdLippe commentedon Jan 30, 2023
Two questions:
mockito-android?zsoltvilagos commentedon Jan 30, 2023
@TimvdLippe I hope @sergiandreplace doesn't mind if I answer.
We're also using Mockito and ran into the very same problem.
Nope. API 33 is the latest version, Android 13.
SmasSive commentedon Jan 30, 2023
@TimvdLippe a colleague of Sergi here! Yes, we are using mockito-android (5.1.0 same version of core).
And as @zsoltvilagos said, no... The latest version of Android (SDK 33) does not include the API.
TimvdLippe commentedon Jan 30, 2023
@reta if I understand correctly, we would have to revert the StackWalker change, but that would break your integration with the
SecurityManager. Can we figure out a solution that would satisfy both?reta commentedon Jan 30, 2023
@TimvdLippe unexpected, but I agree, we probably have to revert at least the
StackWalkerchanges and use the reflection for Android, sorry about that. I could look into it tonight if that is fine with you. Thank you.Yes, I believe we can
Repro for mockito/mockito#2890
TWiStErRob commentedon Jan 30, 2023
Here's a minimal repro in case you want to test things out (or add an integration test for a basic case):
gradlew testDebugUnitTestreport:
./build/reports/tests/testDebugUnitTest/index.htmlgradlew connectedCheckreport:
./build/reports/androidTests/connected/index.htmlgradlew pixel2api33DebugAndroidTestreport:
./build/reports/androidTests/managedDevice/allDevices/index.htmlNote:
connectedCheckandpixel2api33DebugAndroidTestare almost equivalent, butpixel2api33DebugAndroidTestonly needs an SDK installed, it will download and set up the emulator automatically.I used a realistic but pointless mocking setup, also it repros using just
mock()too.Results
Mockito 5.1.0 (committed to repro):
gradlew testDebugUnitTest: ✅ passesgradlew connectedCheck: ❌ failsNoClassDefFoundError: Failed resolution of: Ljava/lang/StackWalker$Option;gradlew pixel2api33DebugAndroidTest: ❌ failsNoClassDefFoundError: Failed resolution of: Ljava/lang/StackWalker$Option;Mockito 5.0.0 (for control, change
def mockitoinbuild.gradle):gradlew testDebugUnitTest: ✅ passesgradlew connectedCheck(API 21): ❌ failsNoClassDefFoundError: org.mockito.internal.invocation.TypeSafeMatching$$ExternalSyntheticLambda0gradlew connectedCheck(API 31): ✅ passesgradlew pixel2api33DebugAndroidTest(API 33): ✅ passesMockito 4.11.0 (for control, change
def mockitoinbuild.gradle):gradlew testDebugUnitTest: ✅ passesgradlew connectedCheck(API 21): ❌ failsNoClassDefFoundError: org.mockito.internal.invocation.TypeSafeMatching$$ExternalSyntheticLambda0gradlew connectedCheck(API 31): ✅ passesgradlew pixel2api33DebugAndroidTest(API 33): ✅ passesFor reference: https://developer.android.com/reference/classes doesn't have
StackWalker.TimvdLippe commentedon Jan 30, 2023
FWIW we started an effort to set up some basic Android tests for us: https://github.com/mockito/mockito/tree/main/subprojects/androidTest However, this stalled as we couldn't get it to work on CI. If anybody affected here would like to dedicate some time to prevent these kind of issues from happening, please take a look at the existing test suite and infrastructure and submit a PR to get it passing on CI.
StackWalker.Option not found on Mockito 5.1.0. Fixes mockito#2890
StackWalker.Option not found on Mockito 5.1.0. Fixes mockito#2890
StackWalker.Option not found on Mockito 5.1.0 (#2891)
zsoltvilagos commentedon Jan 31, 2023
@TimvdLippe @reta Thanks a lot of the awesome fast reaction & fix. Great.
sergiandreplace commentedon Jan 31, 2023
Wow, that was fast. Thanks a lot for the solution!
TWiStErRob commentedon Jan 31, 2023
Confirmed working on the PR I had linked above.
Thanks for the quick turnaround.
Is there a tracking issue for Android CI?
I have a working GHA with Espresso tests (the key was running on Mac):
https://github.com/TWiStErRob/net.twisterrob.colorfilters/blob/master/.github/workflows/CI.yml#L177
reta commentedon Jan 31, 2023
@TWiStErRob thank you!
Yes, #2892
TWiStErRob commentedon Feb 5, 2023
A quick update for watchers and posterity.
The above problem was caused by removing Java 8 support in this PR, and the fix was to partially revert the removal.
After the fix and patch release, we added Android CI with reta (reviewed by TimvdLippe):
@Androids, could you please take a look if you agree the these are sufficient to prevent future breakages like this issue?CI:
mockito/.github/workflows/ci.yml
Lines 88 to 125 in ebdca97
Android Integration test project: https://github.com/mockito/mockito/tree/main/subprojects/androidTest