Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android unit tests can't find Mockito package

I am using Android Studio with Gradle, and I am trying to use Mockito in my unit tests. The problem is that I receive the following errors when I run the tests:

Error:Execution failed for task ':app:compileDebugJava'.

Compilation failed; see the compiler error output for details.

Error:(9, 19) error: package org.mockito does not exist

Error:(11, 26) error: package org.mockito does not exist

Error:(19, 9) error: cannot find symbol variable MockitoAnnotations

My build.gradle file is nothing more than the default with the Mockito dependencies added:

apply plugin: 'android'

android {
    compileSdkVersion 19
    buildToolsVersion "19.0.0"

    defaultConfig {
        minSdkVersion 8
        targetSdkVersion 19
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            runProguard false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    // You must install or update the Support Repository through the SDK manager to use this dependency.
    compile 'com.android.support:appcompat-v7:19.+'


    ////////////////////////////////////////////////
    // These are the only lines I added:

    androidTestCompile 'org.mockito:mockito-core:1.9.5'
    androidTestCompile 'com.google.dexmaker:dexmaker:1.0'
    androidTestCompile 'com.google.dexmaker:dexmaker-mockito:1.0'

    ////////////////////////////////////////////////
}

Then, the lines in my code that are causing the error are just these imports:

import org.mockito.MockitoAnnotations;
import static org.mockito.Mockito.*;

The explanations I've found online all say that the .jar is missing from the CLASSPATH. As far as I understand, Gradle should automatically add the dependencies to the CLASSPATH. Is this correct?

Just to make sure I didn't have to add the jar manually, I also attempted updating the CLASSPATH in my .bashrc file, but this did not work:

export CLASSPATH=/home/myusername/my/jar/path/mockito-all-1.9.5.jar:$CLASSPATH

What am I doing wrong?

like image 451
twiz Avatar asked May 22 '14 23:05

twiz


3 Answers

If you want to run your unit tests with mockito in gradle, the right way to add it to your build.gradle file is:

testCompile 'org.mockito:mockito-core:1.10.19'

Note that it is testCompile and not androidTestCompile

With androidTestCompile you are making mockito available for Android instrumentation tests, not for unit tests.

like image 183
Lucas L. Avatar answered Oct 14 '22 22:10

Lucas L.


Exactly the same issue I faced when I was using "release" build type. Try to switch to the "debug".

like image 29
abel Avatar answered Oct 14 '22 21:10

abel


I tried several different approaches to installing Mockito, following the advice of several sites, but continued to have errors with Gradle build or the test build. I should note that my Android Studio version is 0.6.1. Here is what finally worked for me:

I downloaded the jar files myself and added them to a package in my project structure: ../app/libs:

  • dexmaker-1.0.jar
  • dexmaker-mockito-1.0.jar
  • mockito-all-1.9.5.jar

Then I went to File> Project Structure... and clicked on the Dependencies tab.

From here I could add File dependencies for each of the three jar files. Once added, I changed the Scope to Test compile to prevent them from being included in my APK. After a Gradle Sync, and a Project Clean, I was able to mock classes without issue.

This approach may be a bit more manual than others, but in the end was much simpler than any of the other suggestions I was able to find. Hope this helps.

like image 20
daholt Avatar answered Oct 14 '22 22:10

daholt