11

I try to use Mockito from within Android Studio 1.2.2 but I get the following error:

Error:(50, 17) Failed to resolve: org.mockito:mockito-core:1.10.19

The error occurs when I sync Gradle after adding the dependency manually. This is the dependency in my module Gradle file:

    dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    compile 'com.android.support:appcompat-v7:22.2.0'
    testCompile 'org.mockito:mockito-core:1.10.19' 
}

Could anyone help my resolve this issue?

Related questions:

  1. Do I first need to download Mockito manually?
  2. If so, where should I put it?

Note: the comments were helpfull to solve the above problem. However, it got me into another problem which I could not solve. But updating to Android Studio 1.3 solved it. I am now running Mockito from within Android Studio.

Sweder Schellens
  • 410
  • 1
  • 4
  • 14

4 Answers4

51

Try replacing testCompile with androidTestCompile, it works for me when importing Mockito libs.

However, you may run to a runtime error if you include only mockito-core. You'll need to add into your gradle:

androidTestCompile "org.mockito:mockito-core:1.10.19"
androidTestCompile "com.google.dexmaker:dexmaker:1.2"
androidTestCompile "com.google.dexmaker:dexmaker-mockito:1.2"

If you have error with dexcache, put this line into your setUp() (Assuming you are using InstrumentalTestCase)

System.setProperty("dexmaker.dexcache", getInstrumentation().getTargetContext().getCacheDir().getPath());
Hoang Phan
  • 2,146
  • 1
  • 15
  • 16
  • Unfortunatly, it didn't help. Same error keeps popping up. Actually, I also added the dexcache lines. Now the error comes up 3 times. Do I need to download those packages first? Where should I put them? – Sweder Schellens Jul 14 '15 at 00:26
  • 1
    Probably your gradle is on offline work mode. Go to File/Settings/Gradle and see if "Offline work" checked, uncheck it and sync again. – Hoang Phan Jul 14 '15 at 23:03
  • 1
    I switched the `Offline work` on and off just to check. Now I get this meesage: `...':app:_debugAndroidTestCompile'. > Could not resolve org.mockito:mockito-core:1.10.19 Required by:... > Could not GET 'https://jcenter.bintray.com/org/mockito/mockito-core/1.10.19/mockito-core-1.10.19.pom'.` Any ideas? – Sweder Schellens Jul 14 '15 at 23:33
  • 2
    Ok, look like mockito is a lib of maven. So try adding this `repositories { mavenCentral() }` right before the dependencies block and sync again. – Hoang Phan Jul 15 '15 at 11:56
  • Now I get connection refused for jcenter, mavenCentral and bintray. So I guess repository/dependencies work but I need to check my proxy settings. – Sweder Schellens Jul 18 '15 at 08:09
  • 1
    Did not find any solution. But since the recent update to Android Studio 1.3 it all of the sudden works. – Sweder Schellens Aug 04 '15 at 00:32
  • Converting testCompile to androidTestCompile worked for me. Thanks!!! – Burak Jan 07 '20 at 18:47
  • @HoangPhan thx! that worked was in offline mode, i suspected this might be the case because I didn't see the libs in `External Libraries` in the Project view – marchinram Dec 13 '21 at 21:41
1

I was stuck into a similar problem, and adding the mockito jar file manually did the thing for me.

To do that, first create a directory called "libs" in your app directory. Take note that this directory should be in the same level as that of the src/main and build directories. Next, download the mockito jar file and paste it into the libs directory.

Include that into your dependencies in the app level build.gradle file:

dependencies {
    compile files('libs/add-your-jar-file-name-here')
}

Sync the gradle and that should do the job.

Refer to this answer for more detailed answer with snapshots.

Community
  • 1
  • 1
Akeshwar Jha
  • 4,516
  • 8
  • 52
  • 91
0

androidTestCompile has now been replaced with androidTestImplementation

dependencies {

androidTestImplementation 'junit:junit:4.12'
androidTestImplementation 'org.mockito:mockito-core:1.10.19'
}
heLL0
  • 1,357
  • 3
  • 23
  • 30
-1

Make sure the test file is located under $your_module/src/test/java or $your_module/src/androidTest/java directory.

oɔɯǝɹ
  • 7,219
  • 7
  • 58
  • 69
Watson
  • 1