How to use Mockito/Hamcrest in unit tests in Android Studio - android

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')
}
}

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')
}
}

I had that exact problem too.
I used androidTestCompile instead of testCompile
I also had to have the classpath 'com.android.tools.build:gradle:1.1.0' [or higher]
then i did a buildDependecies before doing a sync.
thats all i did for this error to go away.

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'
}
}

I used androidTestCompile instead of testCompile

Related

Gradle DSL method not found: 'androidtestcompile()'

I am getting an error like this when I am trying to import build.gradle file which I exported from Eclipse
build.gradle code:
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.8.+'
}
}
app module build.gradle which has been generated by Eclipse looks like this
apply plugin: 'android'
dependencies {
compile fileTree(dir: 'libs', include: '*.jar')
compile project(':android-crop-master:lib:src:main')
compile project(':sharebubbles_menu')
compile project(':braintree-1.2.5-project')
compile project(':facebook')
}
android {
compileSdkVersion 19
buildToolsVersion "19.0.1"
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...
instrumentTest.setRoot('tests')
// 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')
}
}
The following is screenshot.
You should update the version of gradle/gradle-plugin/AS that you are using.
Use these versions:
- AS 1.1.0
- Gradle 2.2.1
- Gradle plugin 1.1.0
Then change your build script:
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:1.1.0'
}
}
And your module script:
apply plugin: 'com.android.application'
dependencies {
// The same
}
android {
//Rename your instrumentTest folders to androidTest, e.g. git mv app/src/instrumentTest app/src/androidTest.
// Move the tests to tests/java, tests/res, etc...
androidTestCompile.setRoot('tests')
}
}
All info about the Instrumentation Tests migration is here
I was following the instructions from the Android Developer's page for Instrumented Unit Tests.
https://developer.android.com/training/testing/unit-testing/instrumented-unit-tests.html
I was getting the same error. The errors went away when I moved my edits from the gradle.build (Project: xxx) file to the gradle.build (Module: App).
Thankfully I noticed that there are two gradle.build files or I would still be trying to compile now.

Gradle DSL method not found: "classpath()"

I am working on an Android project, which used Gradle as mentioned below.
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.12.+'
}
}
allprojects {
repositories {
mavenCentral()
}
}
dependencies {
classpath 'com.android.tools.build:gradle:0.12.+'
compile fileTree(dir: 'libs', include: '*.jar')
compile project(':workspace:darkmoon:darul-android:vitamio:vitamio')
compile project(':Dev:adt-bundle-mac-x86_64:sdk:extras:google:google_play_services:libproject:google-play-services_lib')
}
android {
compileSdkVersion 19
buildToolsVersion "20.0.0"
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...
instrumentTest.setRoot('tests')
// 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')
}
}
But when I build it, keep receiving this error: "Gradle DSL method not found", and it pointed to the
following line:
dependencies {
classpath 'com.android.tools.build:gradle:0.12.+'
compile fileTree(dir: 'libs', include: '*.jar')
compile project(':workspace:epsilonmobile:darul-android:vitamio:vitamio')
compile project(':Dev:adt-bundle-mac-x86_64:sdk:extras:google:google_play_services:libproject:google-play-services_lib')
}
Apologize if this question is a bit noob, I'm new to both Gradle and Android Studio
A classpath configuration is only available for buildscript dependencies. You need to get rid of the line classpath 'com.android.tools.build:gradle:0.12.+' in the top-level dependencies block. (Gradle plugins need to be declared under buildscript { dependencies { ... } }.)
In a gradle script, the buildscript is a special section where you can declare dependencies of the build script itself (i.e. binaries required by the build process).
The gradle build process is nothing more than a java process and so it supports normal classpath dependencies.
com.android.tools.build:gradle:0.12.+ identify a binary required by the build process (it contains code able to understand/execute the android section of the build script).
The android apk that will be build by this script don't needs the binary com.android.tools.build:gradle:0.12.+ to run on your android device (i.e. the apk is of course already build when it run on the device) : there is no reason to declare it again in the top level dependencies
(those are the dependencies required by your app)

Add Cardslib library To Android Studio 0.8.1

This is my first project using Android Studio so spare me if you find this question naive. I am trying to include the Cardslib library to my project in Android Studio (version 0.8.1). Initially I tried to include it by adding the following line in build.gradle:
compile 'com.github.gabrielemariotti.cards:library:1.7.3'
But it returned the following error (upon sync)
Error:Failed to find: com.github.gabrielemariotti.cards:library:1.7.3
The I tried to include the jar file by,
Downloading it from maven repository
Adding jar file to libs folder.
Adding following line in build.gradle
compile files('libs/library-1.7.3-sources.jar')`
Though gradle project sync without any error but I am not able to create simple cards i.e still not working for me.
I wanted the first method to work since Android Studio would then handle everything but I guess I am doing something horribly wrong.
[Edit] - Adding the build.gradle code
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.12.+'
}
}
apply plugin: 'android'
dependencies {
compile fileTree(dir: 'libs', include: '*.jar')
compile 'com.github.gabrielemariotti.cards:library:1.7.3'
}
android {
compileSdkVersion 19
buildToolsVersion '20.0.0'
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...
instrumentTest.setRoot('tests')
// 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')
}
}
Add this block in your script to tell gradle where it can find the repo with libs.
repositories {
mavenCentral()
}
So your script will be something like this:
.......
apply plugin: 'android'
repositories {
mavenCentral()
}
dependencies {
compile fileTree(dir: 'libs', include: '*.jar')
compile 'com.github.gabrielemariotti.cards:library:1.7.3'
}
.......

Writing Gradle script to run unit test cases for Eclipse Android Test project

I have a simple HelloWorld Android project (built in Eclipse IDE), I am able to do "gradle build" successfully in cmd prompt for this project.
Also I have written a simple JUnit Android Test Project for it, and it runs fine in Eclipse.
Now I want to run this Test Project or Unit Test Cases (if my understanding is wrong!) using Gradle script.
How do I do that?
Following is the build.gradle file I am using. I want to know how to write script code to automate the test case execution.
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.10.+'
}
}
apply plugin: 'android'
dependencies {
compile fileTree(dir: 'libs', include: '*.jar')
}
android {
compileSdkVersion 19
buildToolsVersion "19.0.3"
sourceSets {
main {
manifest.srcFile 'AndroidManifest.xml'
java.srcDirs = ['src']
resources.srcDirs = ['src']
aidl.srcDirs = ['src']
renderscript.srcDirs = ['src']
res.srcDirs = ['res']
assets.srcDirs = ['assets']
}
test {
java.srcDirs = ['src/test/java']
resources.srcDirs = ['src/test/resources']
}
// Move the tests to tests/java, tests/res, etc...
instrumentTest.setRoot('tests')
androidTest.setRoot('tests')
// 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')
}
}
Here is what I use:
android {
// ...
// your stuff
// ...
// So Android Studio can nicely edit the test files
sourceSets {
androidTest {
java.srcDir file('src/test/java')
}
}
}
// <Task to run tests>
sourceSets {
unitTest {
java.srcDir file('src/test/java')
}
}
configurations {
unitTestCompile.extendsFrom runtime
unitTestRuntime.extendsFrom unitTestCompile
}
task unitTest(type: Test, dependsOn: assemble) {
description = "run unit tests"
testClassesDir = project.sourceSets.unitTest.output.classesDir
classpath = project.sourceSets.unitTest.runtimeClasspath
}
check.dependsOn unitTest
// </Task to run tests>
dependencies {
// ...
// compile stuff
// ....
unitTestCompile files("$project.buildDir/classes/release")
unitTestCompile 'junit:junit:4.11'
}
Run your test:
gradlew unitTest
This solution is not yet implemented and tested. Work in progress, comments are welcomed.
After reading all user guide carefully, it is clear that there is no direct support for Eclipse Android Test projects. The reason is that new build system adopts Gradle(and Maven) style to have Unit test inside module/project.
But because Eclipse Android Test projects is still Android project, there is way to try
Add build.gradle see below to Eclipse Android Test project.
It should have compile project(':app') in dependencies
/* Android
* http://www.nodeclipse.org/projects/gradle
* Nodeclipse/Enide build.gradle template for classic Android project
* https://github.com/Nodeclipse/nodeclipse-1/blob/master/org.nodeclipse.enide.editors.gradle/docs/android/build.gradle
* Gradle Plugin User Guide:
* http://tools.android.com/tech-docs/new-build-system/user-guide
* Usage
* 1. put in project root
* 2. check version numbers
* 3. use from command line or with Gradle for Android http://marketplace.eclipse.org/content/gradle
* Support for this template
* https://github.com/nodeclipse/nodeclipse-1/issues/
* History
* 2014-03-13 android plugin updated to 0.9, see http://tools.android.com/tech-docs/new-build-system/migrating_to_09
* 2014-04-01 check for gradle version
* 2014-04-10 wrapper and runAndroidApplication tasks
* 2014-04-25 rename to run, add <<
* 2014-05-23 defaut plugin version 0.10.x
* 2014-06-06 show "boilerplate part"
* #author Paul Verest
*/
println GradleVersion.current().prettyPrint()
assert gradle.gradleVersion >= "1.10" // android plugin 0.10 requires Gradle 1.10, 1.11, 1.12
// after `gradle wrapper` it is possible to use './gradlew build' with Gradle version specified
task wrapper(type: Wrapper) {
gradleVersion = '1.12'
}
//{ "boilerplate part"
buildscript {
repositories {
mavenCentral()
//jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.10.+'
}
}
apply plugin: 'android'
dependencies {
compile fileTree(dir: 'libs', include: '*.jar')
//androidTestCompile 'com.jayway.android.robotium:robotium-solo:4.3.1'
// for multi-module project build (needs `settings.gradle`):
// reference needed modules or App under test (for Eclipse Android Test project)
compile project(':app')
}
android {
compileSdkVersion 19
buildToolsVersion "19.0.3"
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')
// 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')
}
}
//} "boilerplate part"
Because app and its test now make multi-module project add settings.gradle. The easiest way to put it 1 level down (folder where both project are)
include ':app'
include ':apptest'
Other way is to put in sibling folder, e.g. called parent
includeFlat 'app'
includeFlat 'apptest'
In later case you would need to run build from that folder (or specify each time settings.gradle location with -c CLI option
UPDATE Doing research about pure JUnit tests with gradle (a bit offtopic for this question), it seems like Android Tooling team overlooks this question, breaking what others developers are trying to do with newer android gradle plugin. Even in Android Studio it is not so even How can I create tests in Android Studio? . The only orgnization that cares is Robolectric contributors that have its framework ready for pure JUnit testing https://github.com/robolectric/gradle-android-test-plugin

Could not call incremental task error in Android Studio

So I have bees stuck on this all day on android studio.
I have tried to follow this tutorial to add support for google maps API for my application
https://developers.google.com/maps/documentation/android/start#install_the_android_sdk
but when I try to build the application this error appears
Gradle: Execution failed for task ':dexDebug'.
Could not call IncrementalTask.taskAction() on task ':dexDebug'
here is my build.gradel
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.6.+'
}
}
apply plugin: 'android'
dependencies {
compile 'com.android.support:appcompat-v7:+'
compile 'com.google.android.gms:play-services:4.0.30'
compile fileTree(dir: 'libs', include: '*.jar')
}
android {
compileSdkVersion 16
buildToolsVersion "18.1.1"
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...
instrumentTest.setRoot('tests')
// 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')
}
}
When I remove remove this line for testing the said error doesn't appear.
compile 'com.google.android.gms:play-services:4.0.30'
Does anyone know what the error code be?
Do you have the Play Services installed? In Android Studio, go to the SDK Manager. Under Extras, make sure you have the latest revision of Google Play services. After you install, you may have to restart Android Studio.
To double-check that 4.0.30 was downloaded, you can check that the Android Studio's local Maven repository contains the folder. The path is something like:
/Applications/Android Studio.app/sdk/extras/google/m2repository/com/google/android/gms/4.0.30

Categories

Resources