mockito2 坑 集成使用 Caused by: org.mockito.exceptions.base.MockitoException:
解决方案 compile "org.mockito:mockito-android:2.+" 对应 androidTest
解决方案 compile "org.mockito:mockito-core:2.+" 对应 test
选一个 不能共存
问题描述:
github issues
/Library/Java/JavaVirtualMachines/jdk1.8.0_101.jdk/Contents/Home/bin/java -ea -Didea.launcher.port=7532 "-Didea.launcher.bin.path=/Applications/Android Studio.app/Contents/bin" -Didea.junit.sm_runner -Dfile.encoding=UTF-8 -classpath "/Applications/Android Studio.app/Contents/lib/idea_rt.jar:/Applications/Android Studio.app/Contents/plugins/junit/lib/junit-rt.jar:/Users/heyue/Documents/Work/sdk/platforms/android-25/data/res:/Users/heyue/Documents/pro/MockitoTest/app/build/intermediates/classes/test/debug:/Users/heyue/Documents/pro/MockitoTest/app/build/intermediates/classes/debug:/Users/heyue/.gradle/caches/modules-2/files-2.1/org.mockito/mockito-android/2.6.3/978c9c5098bbfbe3b51048e5bec73bb629b7b40a/mockito-android-2.6.3.jar:/Users/heyue/.gradle/caches/modules-2/files-2.1/com.jakewharton.android.repackaged/libcore-dex/2/1d3c98644912295ca8cb80069b1cdea4389085b2/libcore-dex-2.jar:/Users/heyue/Documents/pro/MockitoTest/app/build/intermediates/exploded-aar/com.android.support/support-fragment/25.0.1/jars/libs/internal_impl-25.0.1.jar:/Users/heyue/Documents/pro/MockitoTest/app/build/intermediates/exploded-aar/com.android.support/support-fragment/25.0.1/jars/classes.jar:/Users/heyue/Documents/pro/MockitoTest/app/build/intermediates/exploded-aar/com.android.support/support-fragment/25.0.1/res:/Users/heyue/.gradle/caches/modules-2/files-2.1/net.bytebuddy/byte-buddy-agent/1.6.2/36d9b5636b5499bb3a62713e080c50b2d4f188da/byte-buddy-agent-1.6.2.jar:/Users/heyue/Documents/pro/MockitoTest/app/build/intermediates/exploded-aar/com.android.support/support-core-ui/25.0.1/jars/classes.jar:/Users/heyue/Documents/pro/MockitoTest/app/build/intermediates/exploded-aar/com.android.support/support-core-ui/25.0.1/res:/Users/heyue/Documents/pro/MockitoTest/app/build/intermediates/exploded-aar/com.android.support/support-core-ui/25.0.1/jars/libs/internal_impl-25.0.1.jar:/Users/heyue/.gradle/caches/modules-2/files-2.1/net.bytebuddy/byte-buddy/1.6.2/f58a01d36e24a94241d44d52c78e380396d5adb2/byte-buddy-1.6.2.jar:/Users/heyue/.gradle/caches/modules-2/files-2.1/org.objenesis/objenesis/2.5/612ecb799912ccf77cba9b3ed8c813da086076e9/objenesis-2.5.jar:/Users/heyue/.gradle/caches/modules-2/files-2.1/com.jakewharton.android.repackaged/dalvik-dx/1/5344672f614ef1df39f7a9d88a5aa0e44d74d4d9/dalvik-dx-1.jar:/Users/heyue/Documents/pro/MockitoTest/app/build/intermediates/exploded-aar/com.android.support/support-core-utils/25.0.1/jars/libs/internal_impl-25.0.1.jar:/Users/heyue/Documents/pro/MockitoTest/app/build/intermediates/exploded-aar/com.android.support/support-core-utils/25.0.1/jars/classes.jar:/Users/heyue/Documents/pro/MockitoTest/app/build/intermediates/exploded-aar/com.android.support/support-core-utils/25.0.1/res:/Users/heyue/Documents/Work/sdk/extras/android/m2repository/com/android/support/support-annotations/25.0.1/support-annotations-25.0.1.jar:/Users/heyue/.gradle/caches/modules-2/files-2.1/org.mockito/mockito-core/2.6.3/92b7b00a8514a0b0aa5481069ea00a37212d3bb5/mockito-core-2.6.3.jar:/Users/heyue/Documents/pro/MockitoTest/app/build/intermediates/exploded-aar/com.android.support/support-compat/25.0.1/res:/Users/heyue/Documents/pro/MockitoTest/app/build/intermediates/exploded-aar/com.android.support/support-compat/25.0.1/jars/classes.jar:/Users/heyue/Documents/pro/MockitoTest/app/build/intermediates/exploded-aar/com.android.support/support-compat/25.0.1/jars/libs/internal_impl-25.0.1.jar:/Users/heyue/Documents/pro/MockitoTest/app/build/intermediates/exploded-aar/com.android.support/animated-vector-drawable/25.0.1/jars/classes.jar:/Users/heyue/Documents/pro/MockitoTest/app/build/intermediates/exploded-aar/com.android.support/animated-vector-drawable/25.0.1/res:/Users/heyue/Documents/pro/MockitoTest/app/build/intermediates/exploded-aar/com.android.support/support-v4/25.0.1/res:/Users/heyue/Documents/pro/MockitoTest/app/build/intermediates/exploded-aar/com.android.support/support-v4/25.0.1/jars/classes.jar:/Users/heyue/.gradle/caches/modules-2/files-2.1/org.hamcrest/hamcrest-core/1.3/42a25dc3219429f0e5d060061f71acb49bf010a0/hamcrest-core-1.3.jar:/Users/heyue/Documents/pro/MockitoTest/app/build/intermediates/exploded-aar/com.android.support/support-media-compat/25.0.1/res:/Users/heyue/Documents/pro/MockitoTest/app/build/intermediates/exploded-aar/com.android.support/support-media-compat/25.0.1/jars/classes.jar:/Users/heyue/Documents/pro/MockitoTest/app/build/intermediates/exploded-aar/com.android.support/support-media-compat/25.0.1/jars/libs/internal_impl-25.0.1.jar:/Users/heyue/.gradle/caches/modules-2/files-2.1/junit/junit/4.12/2973d150c0dc1fefe998f834810d68f278ea58ec/junit-4.12.jar:/Users/heyue/Documents/pro/MockitoTest/app/build/intermediates/exploded-aar/com.android.support/support-vector-drawable/25.0.1/res:/Users/heyue/Documents/pro/MockitoTest/app/build/intermediates/exploded-aar/com.android.support/support-vector-drawable/25.0.1/jars/classes.jar:/Users/heyue/.gradle/caches/modules-2/files-2.1/net.bytebuddy/byte-buddy-android/1.6.2/8e628953a4fb574de34ca130ba0f82f0710121dd/byte-buddy-android-1.6.2.jar:/Users/heyue/Documents/pro/MockitoTest/app/build/intermediates/exploded-aar/com.android.support/appcompat-v7/25.0.1/res:/Users/heyue/Documents/pro/MockitoTest/app/build/intermediates/exploded-aar/com.android.support/appcompat-v7/25.0.1/jars/classes.jar:/Users/heyue/Documents/pro/MockitoTest/app/build/intermediates/sourceFolderJavaResources/test/debug:/Users/heyue/Documents/pro/MockitoTest/app/build/intermediates/sourceFolderJavaResources/debug:/Users/heyue/Documents/pro/MockitoTest/build/generated/mockable-android-25.jar" com.intellij.rt.execution.application.AppMain com.intellij.rt.execution.junit.JUnitStarter -ideVersion5 com.vipheyue.mockitotest.ExampleUnitTest
org.mockito.exceptions.base.MockitoException:
Mockito cannot mock this class: class java.util.ArrayList.
Mockito can only mock non-private & non-final classes.
If you're not sure why you're getting this error, please report to the mailing list.
Java : 1.8
JVM vendor name : Oracle Corporation
JVM vendor version : 25.101-b13
JVM name : Java HotSpot(TM) 64-Bit Server VM
JVM version : 1.8.0_101-b13
JVM info : mixed mode
OS name : Mac OS X
OS version : 10.12
Underlying exception : org.mockito.exceptions.base.MockitoException:
Could not look up implicit location for storing generated classes
You can configure an explicit location by setting the system property
'org.mockito.android.target' to a folder for storing generated class files
This location must be in private scope for most API versions, for example:
MyActivity.this.getDir("target", Context.MODE_PRIVATE)
or
getInstrumentation().getTargetContext().getCacheDir().getPath()
at com.vipheyue.mockitotest.ExampleUnitTest.mockitoTest1(ExampleUnitTest.java:36)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:119)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:42)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:234)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:74)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)
Caused by: org.mockito.exceptions.base.MockitoException:
Could not look up implicit location for storing generated classes
You can configure an explicit location by setting the system property
'org.mockito.android.target' to a folder for storing generated class files
This location must be in private scope for most API versions, for example:
MyActivity.this.getDir("target", Context.MODE_PRIVATE)
or
getInstrumentation().getTargetContext().getCacheDir().getPath()
at net.bytebuddy.TypeCache.findOrInsert(TypeCache.java:138)
at net.bytebuddy.TypeCache$WithInlineExpunction.findOrInsert(TypeCache.java:346)
at net.bytebuddy.TypeCache.findOrInsert(TypeCache.java:161)
at net.bytebuddy.TypeCache$WithInlineExpunction.findOrInsert(TypeCache.java:355)
... 28 more
org.mockito.exceptions.base.MockitoException:
Mockito cannot mock this class: class java.util.LinkedList.
Mockito can only mock non-private & non-final classes.
If you're not sure why you're getting this error, please report to the mailing list.
Java : 1.8
JVM vendor name : Oracle Corporation
JVM vendor version : 25.101-b13
JVM name : Java HotSpot(TM) 64-Bit Server VM
JVM version : 1.8.0_101-b13
JVM info : mixed mode
OS name : Mac OS X
OS version : 10.12
Underlying exception : org.mockito.exceptions.base.MockitoException:
Could not look up implicit location for storing generated classes
You can configure an explicit location by setting the system property
'org.mockito.android.target' to a folder for storing generated class files
This location must be in private scope for most API versions, for example:
MyActivity.this.getDir("target", Context.MODE_PRIVATE)
or
getInstrumentation().getTargetContext().getCacheDir().getPath()
at com.vipheyue.mockitotest.ExampleUnitTest.mockitoWhenReturn(ExampleUnitTest.java:51)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:119)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:42)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:234)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:74)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)
Caused by: org.mockito.exceptions.base.MockitoException:
Could not look up implicit location for storing generated classes
You can configure an explicit location by setting the system property
'org.mockito.android.target' to a folder for storing generated class files
This location must be in private scope for most API versions, for example:
MyActivity.this.getDir("target", Context.MODE_PRIVATE)
or
getInstrumentation().getTargetContext().getCacheDir().getPath()
at net.bytebuddy.TypeCache.findOrInsert(TypeCache.java:138)
at net.bytebuddy.TypeCache$WithInlineExpunction.findOrInsert(TypeCache.java:346)
at net.bytebuddy.TypeCache.findOrInsert(TypeCache.java:161)
at net.bytebuddy.TypeCache$WithInlineExpunction.findOrInsert(TypeCache.java:355)
... 28 more
org.mockito.exceptions.base.MockitoException:
Mockito cannot mock this class: class com.vipheyue.mockitotest.MyBean.
Mockito can only mock non-private & non-final classes.
If you're not sure why you're getting this error, please report to the mailing list.
Java : 1.8
JVM vendor name : Oracle Corporation
JVM vendor version : 25.101-b13
JVM name : Java HotSpot(TM) 64-Bit Server VM
JVM version : 1.8.0_101-b13
JVM info : mixed mode
OS name : Mac OS X
OS version : 10.12
Underlying exception : org.mockito.exceptions.base.MockitoException:
Could not look up implicit location for storing generated classes
You can configure an explicit location by setting the system property
'org.mockito.android.target' to a folder for storing generated class files
This location must be in private scope for most API versions, for example:
MyActivity.this.getDir("target", Context.MODE_PRIVATE)
or
getInstrumentation().getTargetContext().getCacheDir().getPath()
at com.vipheyue.mockitotest.ExampleUnitTest.mockitoWhenReturn2(ExampleUnitTest.java:21)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:119)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:42)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:234)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:74)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)
Caused by: org.mockito.exceptions.base.MockitoException:
Could not look up implicit location for storing generated classes
You can configure an explicit location by setting the system property
'org.mockito.android.target' to a folder for storing generated class files
This location must be in private scope for most API versions, for example:
MyActivity.this.getDir("target", Context.MODE_PRIVATE)
or
getInstrumentation().getTargetContext().getCacheDir().getPath()
at net.bytebuddy.TypeCache.findOrInsert(TypeCache.java:138)
at net.bytebuddy.TypeCache$WithInlineExpunction.findOrInsert(TypeCache.java:346)
at net.bytebuddy.TypeCache.findOrInsert(TypeCache.java:161)
at net.bytebuddy.TypeCache$WithInlineExpunction.findOrInsert(TypeCache.java:355)
... 28 more
Process finished with exit code 255