2

I've started writing the unit tests for my application. I'm using Mockito to mock the objects.

This is the link I followed to include the mockito dependency in my app level gradle file. The problem is I'm unable to import mockito into my test class.

Here's my app build.gradle file for the reference.

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:1.3.0'
        classpath 'com.neenbedankt.gradle.plugins:android-apt:1.+'
    }
}

apply plugin: 'com.android.application'
apply plugin: 'com.neenbedankt.android-apt'

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.1"

    defaultConfig {
        minSdkVersion 14
        targetSdkVersion 23
    }

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_6
        targetCompatibility JavaVersion.VERSION_1_6
    }
    lintOptions {
        disable 'HardcodedText','TextFields','OnClick'
    }
}

repositories {
    jcenter()
    mavenCentral()
    maven() {
        name 'SonaType snapshot repository'
        url 'https://oss.sonatype.org/content/repositories/snapshots'
    }
}

ext {
    robobindingVersion = 'latest.integration'
    //robobindingVersion = '0.8.6-SNAPSHOT'
}

dependencies {
    testCompile "org.mockito:mockito-core:1.+"
    compile("org.robobinding:robobinding:$robobindingVersion:with-dependencies") {
        exclude group: 'com.google.guava', module: 'guava'
    }
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    testCompile('org.robolectric:robolectric:3.0-rc2') {
        exclude group: 'commons-logging', module: 'commons-logging'
        exclude group: 'org.apache.httpcomponents', module: 'httpclient'
    }
    apt "org.robobinding:codegen:$robobindingVersion"
    compile 'junit:junit:4.12'
}

unable to import mockito

Akeshwar Jha
  • 4,516
  • 8
  • 52
  • 91

6 Answers6

4

Try changing

compile "org.mockito:mockito-core:1.+" or implementation "org.mockito:mockito-core:1.+"

to

androidTestImplementation "org.mockito:mockito-core:1.+"

Cameron A
  • 139
  • 1
  • 8
1

I had the same problem and solve it just by using:

compile "org.mockito:mockito-core:1.+"

Hope it helps =]

  • Although this might have worked in your case, and might even work in this case - you really need to add some explanation as to why this should work - or what causes the problem to which this is a solution. – ishmaelMakitla Feb 05 '17 at 20:38
  • This will actually add Mockito to the main build variant of your app. The mockito library will not be used only in tests but is also included in the file that is delivered to your users. Better is to use testImplementation or androidTestImplementation – Janusz Oct 11 '19 at 10:17
0

In my case, I solved it with modify the dependency level from 'testCompile' to 'implementation'

implementation 'org.mockito:mockito-core:5.3.1'
Jos_BCN
  • 1
  • 1
  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Aug 28 '23 at 13:56
0

This question was asked long ago and I assume it is no longer relevant for the asker. And I see a lot of answers that might work or solve and indiviuals problem and the right answer is among them. However, I did not see any answer yet that explains the reason why the asker of the original question was unable to import mockito.

The reason is that they declared the dependency on mockito as testCompile. Next they tried to import them in an InstrumentationTestCase. These have a different dependency set. OP should have declared the depencency using androidTestCompile.

Furthermore, the keywords have changed since this question was asked. Now we use implementation and api to declare module dependencies.

Use implementation when you do not need access to the dependencies of the module you depend on. You will use this most times. Use api if you do need the dependencies the module depends on yourself.

Again, use no prefix if you need it in the modules logic. Use the test prefix if you need the dependency for local unit testing code (testImplementation "org.mockito...") and use the androidTest prefix if you need it in instrumentation tests (androidTestImplementation "org.mockito...").

stephanmantel
  • 1,807
  • 4
  • 16
  • 26
-1

Manually importing the mockito jar file 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
-1

Check this link .... this may useful
https://stackoverflow.com/a/34148132/5185201

// add dexOptions

dexOptions {
    incremental true
    javaMaxHeapSize "4g"
}

// Enabling multidex support.

multiDexEnabled true
Community
  • 1
  • 1
Anvesh523
  • 368
  • 6
  • 18