9

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?

Community
  • 1
  • 1
twiz
  • 9,041
  • 8
  • 52
  • 84
  • I figured out that if I change `androidTestCompile` to just `compile`, everything works fine. I'm not sure if this is the correct usage though. Am I just completely misunderstanding the purpose of `androidTestCompile`? – twiz May 23 '14 at 03:18
  • Be careful since compile will include mockito into your apk. Which is not your intention I think – Eugen Martynov May 26 '14 at 20:10
  • What kind of unit tests you have - robolectric or Instrumental? – Eugen Martynov May 26 '14 at 20:10

4 Answers4

9

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.

Lucas L.
  • 1,001
  • 11
  • 19
2

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

abel
  • 156
  • 1
  • 5
1

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.

daholt
  • 11
  • 2
0

I had the same issue and it was a pretty simple fix;

You may need to synchronize the gradle dependencies.

On the bar to the right you'll see the gradle tool bar (If not you find it under View > Tools Window > Gradle in the top menu bar). Click the refresh symbol and it will download it for you.

Gemtastic
  • 6,253
  • 6
  • 34
  • 53