Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Studio (Gradle) cannot find Mockito

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.

like image 570
Sweder Schellens Avatar asked Jul 04 '15 10:07

Sweder Schellens


2 Answers

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());
like image 50
Hoang Phan Avatar answered Oct 02 '22 21:10

Hoang Phan


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.

like image 36
Akeshwar Jha Avatar answered Oct 02 '22 23:10

Akeshwar Jha