Android Studio (Gradle) cannot find Mockito

后端 未结 4 1622
余生分开走
余生分开走 2020-12-11 16:39

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.1

相关标签:
4条回答
  • 2020-12-11 16:50

    androidTestCompile has now been replaced with androidTestImplementation

    dependencies {
    
    androidTestImplementation 'junit:junit:4.12'
    androidTestImplementation 'org.mockito:mockito-core:1.10.19'
    }
    
    0 讨论(0)
  • 2020-12-11 16:51

    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.

    0 讨论(0)
  • 2020-12-11 16:56

    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());
    
    0 讨论(0)
  • 2020-12-11 17:05

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

    0 讨论(0)
提交回复
热议问题