New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Byte buddy exceptions cause Android instrumented tests to fail. #922
Comments
I also faced this issue and found a workaround. WorkaroundAdd the following AndroidManifest.xml to your androidTest directory. <!--
The workaround for Mockito issue #922
https://github.com/mockito/mockito/issues/922
-->
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package=[your test package]>
<!--suppress AndroidDomInspection -->
<instrumentation>
<!--suppress AndroidElementNotAllowed -->
<meta-data
android:name="notPackage"
android:value="net.bytebuddy" />
</instrumentation>
</manifest> Or if using your own AndroidJUnitRunner, you can also set the notPackage option on onCreate(). public class YourAndroidJUnitRunner extends AndroidJUnitRunner {
@Override
public void onCreate(Bundle arguments) {
/*
The workaround for Mockito issue #922
https://github.com/mockito/mockito/issues/922
*/
arguments.putString("notPackage", "net.bytebuddy");
super.onCreate(arguments);
}
} |
@tmurakami Wonderful. Thanks for the workaround. Fixed the issue for me. |
The problem is that the Android test suite scans all classes and attempts loading all of them. The Mockito and Byte Buddy dependencies should however be off limits as they contain code that the Android runtime does not support, therefore, the explicit exclusion fixes the problem. I am currently looking for a way of letting Android know from the library that we will not contain any test sources. This is done by the explicit runner exclusion but I hoped that there was a property to do so. If this is not possible, I am afraid, then the suggested solution is the only way to do so in which case this should become part of the documentation. |
I have made the workaround library for this issue. @raphw If it is possible to change the mockito-android artifact to an android-gradle project, you can solve this issue just by placing |
Thanks for the detailed description of a work-around! I will try to integrate this tonight. If you find the time, feel free to submit a PR. @mockito/developers Is this Android dependency for the artifact possible in our build? Or do you think we should rather add information to the docs. |
@tmurakami Could you have a look at the |
@raphw I have tried to build the android-bundle branch.
I suppose that you have to prepare these files for Android.
In addition, you probably need to change travis.yml to build Android project. I think I can add gradle files for Android project. |
Please do! If we get the artifact right, we can adjust the Travis build from there! Thanks a lot! |
FYI, in relation to #943, shading Byte Buddy will probably solve this issue (and PR #938 will be unnecessary). In the latest test runner // Excluded test packages
private static final String[] DEFAULT_EXCLUDED_PACKAGES = {
"junit",
"org.junit",
"org.hamcrest",
"org.mockito",// exclude Mockito for performance and to prevent JVM related errors
"android.support.test.internal.runner.junit3",// always skip AndroidTestSuite
"org.jacoco"// exclude Jacoco to prevent class loading issues
}; from |
I did not know about the explicit exclusion but I did wonder why it did not reject Mockito while investigating this. Ideally, they should exclude Byte Buddy and cglib, too. |
@andrew-carmichael Sorry, the above workaround is bad. Instead, run
@raphw Please do not merge PR #938 to |
Thanks for the update. I will put this into the documentation and hopefully, the Android folks can exclude the Byte Buddy namespace, too. I would not know how to file an issue there, but I will look into it. Otherwise, feel free to do it yourself. |
@raphw If you want, I will report this issue to the AOSP team. |
@tmurakami Please do, thanks a lot! |
I have just reported to the AOSP team. |
any updates on this issue? |
@knutvalen, I filed a patch, but unfortunately I have no reply from the AOSP team... |
With test runner 1.0.0, this issue seems to be solved. repositories {
google()
}
dependencies {
androidTestCompile 'com.android.support.test:runner:1.0.0'
} |
Yes upgrading to test runner 1.0.0 has fixed the issue for us as well. |
@tmurakami Do you need to upgrade an Android project to Android Studio 3 in order to use test runner 1.0.0 or 1.0.1? UPDATE Adding the following to app build.gradle worked in Android Studio 2:
|
The Google repository has been added since Gradle 4.0. If you use Gradle < 4.0, you need to add |
I think this issue should be closed because it is solved in Android Testing Support Library 1.0.0. |
Closing per above comment. |
I'm proposing this issue be opened again. It hasn't been fixed on Android Testing Support Library 1.0.0. I've actually tested with 1.0.2 and it is still there. Also, the above AOSP patch hasn't been merged either. |
I do not currently have much time to test this myself, does Byte Buddy fail to inject classes? I have only followed the API instructions. Could you provide a stack trace with the latest version? |
Sorry for my late reply. |
Nothing helped but downgrading mockito to 2.22.0 |
We're seeing this now from moving from testing 1.0.2 to 1.1.1 (via androidx) |
Any update on this? |
Are you trying with recent version of Byte Buddy? This should no longer be an issue. |
I was using an old(default) test runner, updating to the new test runner fixed it for me. Thanks. |
Closing this once again after inactivity. |
Background:
I attempted to upgrade a fairly large project from mockitio 1x to 2x. I discovered that mockito 2 tests work only when run using the gradle wrapper, not when launched more directly with adb. The gradle wrapper is sufficient for smaller projects but we are using adb for its ability to shard tests across multiple agents. Mockito 1.x does not have this limitation.
Environment:
OS: OSX 10.12.2
IDE: Android Studio 2.3 Beta 2
Android Device: Emulator API 24
Steps:
NOTE) A single empty unit test has been created on our behalf.
androidTestCompile org.mockito:mockito-android:2.6.3"
)/gradlew clean installDebug installDebugAndroidTest
./gradlew connectedAndroidTest
./gradlew installDebug installDebugAndroidTest
adb shell pm list instrumentation
, locate test app in outputexample: instrumentation:com.example.countolaf.scheme.test/android.support.test.runner.AndroidJUnitRunner (target=com.example.countolaf.scheme)
package/runner
portion (com.example.countolaf.scheme.test/android.support.test.runner.AndroidJUnitRunner
)adb shell am instrument -w com.example.countolaf.scheme.test/android.support.test.runner.AndroidJUnitRunner
RESULT)
The same exceptions have occurred but are considered test failures.
EDIT by @bric3 : improved markdown formatting for better readability
The text was updated successfully, but these errors were encountered: