How to use Mockito/Hamcrest in unit tests in Android Studio
Asked Answered
A

4

5

I want to be able to make unit tests and instrumentation tests in Android Studio, and using Mockito in them.

I'm using the new approach for tests in Android Studio 0.8. This is:

  • building with gradle
  • using official Android API for testing (ActivityInstrumentationTestCase2, etc)
  • having the tests inside the directory of the app, not as a separate module
  • launching the tests in Android Studio as a "Android Test" run configuration

How can I write code in my tests that depends on libraries used only for the tests, such as mockito or hamcrest?

I'd like to include these libraries when compiling and running the tests, but avoid them to be exported to the released .apk.

In https://code.google.com/p/mockito/wiki/DeclaringMockitoDependency I've read that I should add the dependency as:

dependencies {
    ....
    testCompile "org.mockito:mockito-core:1.9.5"
}

But when running I get:

Build script error, Unsupported Gradle DSL method found: 'testCompile()'!

Although I'm not sure it's relevant, the gradle build file I'm using is:

apply plugin: 'android'

dependencies {
    compile fileTree(dir: 'libs', include: '*.jar')
    compile project(':android-sdk')
    testCompile "org.mockito:mockito-core:1.9.5"
}

android {
    compileSdkVersion 19
    buildToolsVersion "20.0.0"

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_7
        targetCompatibility JavaVersion.VERSION_1_7
    }

    sourceSets {
        main {
            manifest.srcFile 'AndroidManifest.xml'
            java.srcDirs = ['src']
            resources.srcDirs = ['src']
            aidl.srcDirs = ['src']
            renderscript.srcDirs = ['src']
            res.srcDirs = ['res']
            assets.srcDirs = ['assets']
        }

        // Move the tests to tests/java, tests/res, etc...
        androidTest.setRoot('tests')

        // Note - to run the tests from command line:
        // $ gradle clean connectedCheck build
        // (requires gradle 1.10)

        // Move the build types to build-types/<type>
        // For instance, build-types/debug/java, build-types/debug/AndroidManifest.xml, ...
        // This moves them out of them default location under src/<type>/... which would
        // conflict with src/ being used by the main source set.
        // Adding new build types or product flavors should be accompanied
        // by a similar customization.
        debug.setRoot('build-types/debug')
        release.setRoot('build-types/release')
    }
}
Araceli answered 18/8, 2014 at 16:49 Comment(0)
A
7

I got it working by using the "androidTestCompile" options under "dependencies", as explained here.

What I have done is:

  • created a folder called libs-tests with the jars that should only be used for testing.
  • added that folder as a dependency for tests with "androidTestCompile"

Now, the gradle build file stands as:

apply plugin: 'android'

dependencies {
    compile project(':android-sdk')

    // The libs folder is included in the apk of the real app
    compile fileTree(dir: 'libs', include: '*.jar')

    // The tests-libs folder is included only for tests
    androidTestCompile fileTree(dir: 'libs-tests', include: '*.jar')
}


android {
    compileSdkVersion 19
    buildToolsVersion "20.0.0"

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_7
        targetCompatibility JavaVersion.VERSION_1_7
    }


    sourceSets {
        main {
            manifest.srcFile 'AndroidManifest.xml'
            java.srcDirs = ['src']
            resources.srcDirs = ['src']
            aidl.srcDirs = ['src']
            renderscript.srcDirs = ['src']
            res.srcDirs = ['res']
            assets.srcDirs = ['assets']
        }

        // Move the tests to tests/java, tests/res, etc...
        androidTest.setRoot('tests')

        // Note - to run the tests from command line:
        // $ gradle clean connectedCheck build
        // (requires gradle 1.10)

        // Move the build types to build-types/<type>
        // For instance, build-types/debug/java, build-types/debug/AndroidManifest.xml, ...
        // This moves them out of them default location under src/<type>/... which would
        // conflict with src/ being used by the main source set.
        // Adding new build types or product flavors should be accompanied
        // by a similar customization.
        debug.setRoot('build-types/debug')
        release.setRoot('build-types/release')
    }
}
Araceli answered 19/8, 2014 at 12:15 Comment(0)
P
4

I had that exact problem too.

  1. I used androidTestCompile instead of testCompile
  2. I also had to have the classpath 'com.android.tools.build:gradle:1.1.0' [or higher]
  3. then i did a buildDependecies before doing a sync.

thats all i did for this error to go away.

Poi answered 1/4, 2015 at 8:17 Comment(0)
C
2

Since the android gradle plugin version 1.1.0, there is support for unit testing so you can use testCompile in the gradle files. Turn it on with settings > gradle > experimental and update the gradle plugin version:

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:1.1.0'
    }
}
Comus answered 10/3, 2015 at 9:55 Comment(0)
L
0

I used androidTestCompile instead of testCompile

Laden answered 24/3, 2016 at 6:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.