question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

i use public class but can't mock instance

See original GitHub issue
public class MyBean {
    public String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

logcat

/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

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:12 (7 by maintainers)

github_iconTop GitHub Comments

5reactions
bric3commented, Jan 19, 2017

@vipheyue I think mockito-android should be declared like dexmaker was, i.e. with androidTestCompile scope.

dependencies {
    testCompile 'junit:junit:4.12'
    testCompile 'org.mockito:mockito-core:2.+'
    androidTestCompile 'org.mockito:mockito-core:2.+'
    androidTestCompile "org.mockito:mockito-android:2.+"
}
3reactions
TimvdLippecommented, Jan 19, 2017
Read more comments on GitHub >

github_iconTop Results From Across the Web

Why is my class not calling my mocked methods in unit test?
Mocks are set up, but then a different instance is used. Or a mock instance is created and injected into the SUT (potentially...
Read more >
Creating Mocks and Spies in Mockito with Code Examples
For all the Mock and Spy description, we are referring to a fictitious class/object called 'DiscountCalculator' which we want to mock/spy.
Read more >
A Unit Testing Practitioner's Guide to Everyday Mockito - Toptal
Using our example code, let's look at how to apply Mockito and write some tests. ... Out of the box, Mockito cannot mock...
Read more >
Mocking Non-public Members and Types | Telerik JustMock
You can use JustMock to mock non-public members and types in elevated mode. That is useful when you want to isolate calls to...
Read more >
Mock Final Classes and Methods with Mockito - Baeldung
Similar to the test above, we create a concrete instance and a mock instance of our final class, mock a method and verify...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found