Closed jaredsburrows closed 7 years ago
The number of methods in the actual dexmaker project hasn't changed...from what I can tell, this is due to upgrading to a newer version of Mockito, which depends transitively on bytebuddy.
I did some investigation and opened this issue on the mockito repo: https://github.com/mockito/mockito/issues/1112
If they can reduce their method count, we can also update dexmaker to pull in the newer version of Mockito.
Hi.
@jaredsburrows, DexMaker has its own MockMaker, so I think that you can exclude Byte Buddy from your test apk.
dependencies {
androidTestCompile('com.linkedin.dexmaker:dexmaker-mockito:x.y.z') {
exclude group: 'net.bytebuddy'
}
}
http://www.methodscount.com/?lib=com.linkedin.dexmaker%3Adexmaker-mockito%3A%2B
Thanks. Makes sense to me!
Please let me know if that solution works for you, I'm very interested to find out.
@drewhannay I just wanted to confirm that it works by excluding net.bytebuddy
.
@tmurakami Thank you. That works! Does this mean I cannot mock final
when using Mockito 2?
@jaredsburrows, you can still use Mockito 2 with Byte Buddy to mock final classes for local unit testing.
dependencies {
// For local unit tests (on your develop machine)
testCompile 'org.mockito:mockito-inline:x.y.z'
// For instrumented unit tests (on Android emulators and devices)
androidTestCompile('com.linkedin.dexmaker:dexmaker-mockito:x.y.z') {
exclude group: 'net.bytebuddy'
}
}
If you are using Kotlin and would like to mock final classes on Android emulators and devices, please refer to https://github.com/mockito/mockito/issues/1082#issuecomment-301646307.
I use Mockito 2 just fine in unit tests(testCompile
), but if we exclude net.bytebuddy
from dexmaker-mockito
, how can you mock final
? Isn't net.bytebuddy
for code generation? Why does Mockito 2 include as a transitive dependency?
Also, thanks again!
@jaredsburrows,
dependencies {
// For local unit tests (on your develop machine)
testCompile 'org.mockito:mockito-inline:2.8.9'
// For instrumented unit tests (on Android emulators and devices)
androidTestCompile('com.linkedin.dexmaker:dexmaker-mockito:2.2.0') {
exclude group: 'net.bytebuddy'
}
}
Running ./gradlew androidDependencies
with the above code will get the following result.
$ ./gradlew androidDependencies
NDK is missing a "platforms" directory.
If you are using NDK, verify the ndk.dir is set to a valid NDK directory. It is currently set to /home/tmura/android-sdk-linux/ndk-bundle.
If you are not using NDK, unset the NDK variable from ANDROID_NDK_HOME or local.properties to remove this warning.
The ConfigurableReport.setDestination(Object) method has been deprecated and is scheduled to be removed in Gradle 4.0. Please use the method ConfigurableReport.setDestination(File) instead.
:app:androidDependencies
debug
No dependencies
debugAndroidTest
\--- com.linkedin.dexmaker:dexmaker-mockito:2.2.0@jar
+--- com.linkedin.dexmaker:dexmaker:2.2.0@jar
| \--- com.jakewharton.android.repackaged:dalvik-dx:7.1.0_r7@jar
| \--- com.jakewharton.android.repackaged:libcore-dex:7.1.0_r7@jar
\--- org.mockito:mockito-core:2.2.29@jar
\--- org.objenesis:objenesis:2.4@jar
debugUnitTest
\--- org.mockito:mockito-inline:2.8.9@jar
\--- org.mockito:mockito-core:2.8.9@jar
+--- net.bytebuddy:byte-buddy:1.6.14@jar
+--- net.bytebuddy:byte-buddy-agent:1.6.14@jar
\--- org.objenesis:objenesis:2.5@jar
release
No dependencies
releaseUnitTest
\--- org.mockito:mockito-inline:2.8.9@jar
\--- org.mockito:mockito-core:2.8.9@jar
+--- net.bytebuddy:byte-buddy:1.6.14@jar
+--- net.bytebuddy:byte-buddy-agent:1.6.14@jar
\--- org.objenesis:objenesis:2.5@jar
BUILD SUCCESSFUL in 7s
1 actionable task: 1 executed, 0 avoided (0%)
Byte Buddy is used for MockMaker
of Mockito, so it is required for local unit testing.
However, since DexMaker has its own MockMaker
which does not rely on Byte Buddy, it can be excluded from dependencies scoped as androidTestCompile
.
FYI, Mockito does not support mock final on Android.
https://static.javadoc.io/org.mockito/mockito-core/2.8.9/org/mockito/Mockito.html#0.1
Be aware that you cannot use the inline mock maker on Android due to limitations in the Android VM.
Because inline mock maker
relies on the java.lang.instrument
API which Android does not support.
Great explantation! Thanks again!
Byte Buddy is used for MockMaker of Mockito, so it is required for local unit testing. However, since DexMaker has its own MockMaker which does not rely on Byte Buddy, it can be excluded from dependencies scoped as androidTestCompile.
FYI, Mockito does not support mock final on Android.
Explanation:
Swapping out Google's dexmaker with LinkedIn's dexmaker, I am getting the following error. From what I can tell, the LinkedIn version of the dexmaker is so much larger that it causes the Android Test APK to become Multidex.
I have tried the following: https://stackoverflow.com/questions/30081386/how-to-put-specific-classes-into-main-dex-file and https://stackoverflow.com/questions/34310259/trying-to-test-an-android-module-in-multidex-app-com-android-test-runner-multid.
Any way to avoid this?
Error: