How to setup the robolectric in Android Studio - android

I am doing work to test my android application using unit framework robolectric. I have installed the Android Studio (.4.6)
All blogs saying for this "In order to be able to run Android unit tests with Gradle, we need to add the Gradle Android Test plug-in to the build script."
but that is deprecated now then how can I setup this without using this or I have to use this.

I am using com.github.jcandksolutions.gradle:android-unit-test:+
So in your root build.gradle (buildscript section):
repositories {
mavenLocal()
mavenCentral()
maven { url 'https://oss.sonatype.org/content/repositories/snapshots/' }
}
dependencies {
classpath 'com.android.tools.build:gradle:0.8.+'
classpath 'com.github.jcandksolutions.gradle:android-unit-test:+'
}
In your app's build.gradle
apply plugin: 'android'
android {
[...]
sourceSets {
// this sets the root test folder to src/test overriding the default src/instrumentTest
instrumentTest.setRoot('src/test')
}
}
apply plugin: 'android-unit-test'
dependencies {
// example dependencies
instrumentTestCompile 'junit:junit:4.+'
instrumentTestCompile 'org.robolectric:robolectric:2.3-SNAPSHOT'
testCompile 'junit:junit:4.+'
testCompile 'org.robolectric:robolectric:2.3-SNAPSHOT'
}
Note that you have to declare the dependency twice (one for instrumentTestCompile scope and one for testCompile scope (for android-unit-test plugin)). This is necessary at least for this version of Android Studio and the plugin.
Then you can run tests with gradlew test from terminal (either in Android Studio or standalone).
Side note 1: I had some problems with Android Studio terminal integration on Windows. It did not handle well the limited horizontal space available, truncating the output. As a result I started using ConEmu, avoiding the embedded terminal in Android Studio and the standard cmd.exe.

Related

Could not get resource for Android Architecture Components

I applied to my project Android Architecture Components, by adding this lines to build.gradle:
// Android Architecture Lifecycle
compile "android.arch.lifecycle:runtime:1.0.0-alpha1"
compile "android.arch.lifecycle:extensions:1.0.0-alpha1"
annotationProcessor "android.arch.lifecycle:compiler:1.0.0-alpha1"
// Android Architecture Room
compile "android.arch.persistence.room:runtime:1.0.0-alpha1"
compile "android.arch.persistence.room:rxjava2:1.0.0-alpha1"
annotationProcessor "android.arch.persistence.room:compiler:1.0.0-alpha1"
It worked, but after updating Android Studio to Canary 3 version I'm still getting this error while compiling
Error:org.gradle.api.resources.ResourceException: Could not get
resource
'https://jitpack.io/android/arch/lifecycle/runtime/1.0.0-alpha1/runtime-1.0.0-alpha1.pom'.
Error:org.gradle.api.UncheckedIOException: Could not HEAD
'https://jitpack.io/android/arch/lifecycle/runtime/1.0.0-alpha1/runtime-1.0.0-alpha1.pom'.
Received status code 401 from server: Unauthorized
... other poms from library with the same error.
I tried restarting Android Studio, uninstalling app and of course clean-rebuild.
You need to add the new public Maven repo that Google is using to your build.gradle file.
For example, you could add it to the allprojects closure in the top-level build.gradle file:
allprojects {
repositories {
jcenter()
maven { url 'https://maven.google.com' }
}
}
Then, all of your modules (e.g., app/) will know to look there in addition to other places for the artifacts.
From your error message, it would appear that Android Studio is only looking in jitpack.io.

Android TDD: The saga continues with Robolectric & Gradle

Just when I'd achieved an effective development and build environment with the android-maven-plugin, the new kid on the block, Gradle, starts making inroads into the Android circles. Not being hot on Groovy and with the android-gradle plugin almost as fragmented as the OS itself I've hit some issues. Specifically around building library projects, with flavours and our buddy Robolectric.
Short version
I am at a loss as to what my next move should be upon encountering the gradle error;
Cannot add a SourceSet with name 'testDebug' as a SourceSet with that name already exists.
The error emanates from having productFlavours on a library (i.e. moving to the 0.9.2 android build system) and the gradle-android-test-plugin recently forked by the team over at Robolectric from Jake's creation (see here). I have followed all lines of investigation to near exhaustion and can report that the meer existence of the 'android-test' plugin within my library gradle file sends things awry.
Longer version
Here is the abridged application build.gradle file with pertinent information retained;
apply plugin: 'android-library'
apply plugin: 'android-test'
...
android {
compileSdkVersion 19
buildToolsVersion '19.0.3'
defaultConfig {
minSdkVersion 18
targetSdkVersion 19
versionCode buildNumber().toInteger()
versionName "1.0.0"
}
productFlavors {
estimote {
dependencies {
compile '<flavour specific dependency>'
}
}
radius {
dependencies {
compile '<flavour specific dependency>'
}
}
}
}
...
dependencies {
compile 'com.squareup.dagger:dagger:1.2.1'
compile 'com.squareup.dagger:dagger-compiler:1.2.1'
compile 'com.google.code.gson:gson:2.2.+'
// Testing dependencies
androidTestCompile 'org.mockito:mockito-core:1.9.5'
androidTestCompile 'com.squareup:fest-android:1.0.7'
androidTestCompile 'org.hamcrest:hamcrest-all:1.3'
androidTestCompile 'org.robolectric:robolectric:2.2'
androidTestCompile 'junit:junit:4.11'
}
And here is the abridged root build.gradle file.
buildscript {
repositories {
mavenLocal()
mavenCentral()
maven {
url 'https://oss.sonatype.org/content/repositories/snapshots'
}
}
dependencies {
classpath 'com.android.tools.build:gradle:0.9.2'
classpath 'org.robolectric.gradle:gradle-android-test-plugin:0.9.+'
}
}
allprojects {
repositories {
mavenLocal()
mavenCentral()
maven {
url 'https://oss.sonatype.org/content/repositories/snapshots'
}
}
}
If you've got this far down the page, give yourself a pat on the back. Now, the eagle eyed amongst you have probably noticed the omission on the sourceSets redirection with commands akin to;
sourceSets {
androidTest {
setRoot('src/test')
}
}
After the initial error is corrected these lines will need to be reinstated to inform gradle of the project's structure. The project's structure is standard and looks like;
- project_name
+ gradle
- lib
+ flavour1
+ flavour2
- main
+ java
- test
+ java
build.gradle
build.gradle
gradle.properties
settings.gradle
What is being used
The app is using gradle-1.10-all, 0.9.2 android-gradle plugin and 0.9.+ gradle-android-test-plugin.
The question
How should the project be set-up/changed to facilitate Robolectric testing on a library with flavours? Is this even possible yet?
I ran into the same issue, dug into the code, fixed it, and submitted a pull request which has just now been merged. See my explanation on the PR for details, but it boils down to a bad optimization in the plugin code:
https://github.com/robolectric/robolectric-gradle-plugin/pull/70
As of today you need to clone the repo and build and install the plugin to your local maven repo. The next time they do a release to maven central (perhaps release 0.13.1?), you'll be able to use the plugin directly from there.

Error when setting up unit testing with Gradle

I'm trying to set up Android unit testing following this tutorial: http://www.peterfriese.de/android-testing-with-robolectric/
But then I realized I don't know enough about the build system to understand what's really going on.
So I have some questions about part of my build.gradle below
buildscript {
repositories {
mavenCentral()
maven {
url 'https://oss.sonatype.org/content/repositories/snapshots/'
}
}
dependencies {
classpath 'com.android.tools.build:gradle:0.6.+'
classpath 'com.squareup.gradle:gradle-android-test-plugin:0.9.1-SNAPSHOT'
}
}
and
dependencies {
compile 'com.android.support:appcompat-v7:+'
testCompile 'junit:junit:4.10'
testCompile 'org.robolectric:robolectric:2.3-SNAPSHOT'
testCompile 'com.squareup:fest-android:1.0.+'
instrumentTestCompile 'junit:junit:4.10'
instrumentTestCompile 'org.robolectric:robolectric:2.3-SNAPSHOT'
instrumentTestCompile 'com.squareup:fest-android:1.0.+'
}
When I do classpath 'com.squareup.gradle:gradle-android-test-plugin:0.9.1-SNAPSHOT', how does Gradle know where com.squareup.gradle is?
Also, right now, I get an error saying Gradle can't find the Robolectric library org.robolectric:robolectric:2.3-SNAPSHOT. Is it something I have to manually add or is the build system supposed to somehow automatically take care of it?
Gradle will find com.squareup.gradle from the configured repositories -- since it doesn't exist in Maven Central, it'll look in the Sonatype repo and find it there.
The Robolectric library should be available to Gradle, as visiting the snapshot repository URL manually shows it to be there. You could have an earlier version cached: mark the repository as 'changing', refresh dependencies, or simply rm -rf .gradle/caches to force Gradle to look again. See this answer for more: How can I force gradle to redownload dependencies?

In Android Gradle, is there any way to use local snapshot dependencies?

My Android app is based on Gradle and it just takes ages to build every time. This is due to the number of modules I have. Even if there are no changes in submodules, it keeps rebuilding every sources.
I was wondering if there is any way to convert these modules to local snapshot dependencies as I'm not updating them often?
I'm pretty sure it's possible but I have a very basic experience with gradle and maven so I can't figure out a simple way to do that.
Basically right now I'm listing my dependencies like that:
dependencies {
compile project(':Library:lib1')
compile project(':Library:lib2')
compile project(':Library:lib3')
}
and I'd like to use something like that:
repositories {
local()
}
dependencies {
compile 'com.lib1:lib:SNAPSHOT-1.0')
compile 'com.lib2:lib:SNAPSHOT-1.0')
compile 'com.lib3:lib:SNAPSHOT-1.0')
}
To use local snapshots use the maven-publish plugin. If you use SNAPSHOT in the version (e.g. 0.0.1-SNAPSHOT) you will publish snapshots to your local repository. For the build.gradle for lib1 you should do something like this:
apply plugin: 'java'
apply plugin: 'maven-publish'
project.version=0.0.1-SNAPSHOT
publishing {
publications {
maven(MavenPublication) {
from components.java
}
}
}
and run the :publishMavenPublicationToMavenLocal target.
In you gradle build file for projects using the library use:
repositories {
local()
}
dependencies {
compile group: 'com.lib1', name: 'lib', version: 'SNAPSHOT-0.0.1', changing: true
}
The 'changing' attribute indicates that not a cached version is used (normally updated once every 24hrs) but always checks for the latest.

junit testing with gradle for an android project

I am trying to get tests ( junit and robolectric ) working in an Android project but am totally stuck. My main problem is that all testing I found with gradle somehow pull in the java plugin and then I get this error:
The 'java' plugin has been applied, but it is not compatible with the Android plugins.
The only way out I see at the moment is to split into test and app project - but I would like to avoid that. Any examples/hints would be highly appreciated!
In the official documentation there is no mention of unit-testing - only Instrumentation-Tests - but I want unit-tests to get results fast.
You don't need the Java plugin, since the Android will take care of what you need mostly, from what I've seen so far.
I managed to get my Robolectric and junit tests running via this man's blog: http://tryge.com/2013/02/28/android-gradle-build/
My build.gradle file looks like this (where my test files are in the {projectdir}/test directory.
...
// Unit tests
sourceSets {
unitTest {
java.srcDir file('test')
resources.srcDir file('test/resources')
}
}
dependencies {
unitTestCompile files("$project.buildDir/classes/debug")
unitTestCompile 'junit:junit:4.11'
unitTestCompile 'org.robolectric:robolectric:2.1.1'
unitTestCompile 'com.google.android:android:4.0.1.2'
}
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
}
build.dependsOn unitTest
AndroidStudio and the new Android Gradle plugin are now offering official unit test support.
This is supported from Android Studio 1.1+ and Android Gradle plugin version 1.1.0+
Dependencies can now be declared as testCompile:
dependencies {
testCompile 'junit:junit:4.12'
testCompile "org.mockito:mockito-core:1.9.5"
}
More details here: Unit testing support - Android Tools Project Site.
This guide might help -
http://www.slideshare.net/tobiaspreuss/how-to-setup-unit-testing-in-android-studio
Latest gradle the test should be under androidTest dir
Also in your gradle.build:
dependencies {
androidTestCompile 'junit:junit:4.+'
}
also add those under defaultConfig {
testPackageName "test.java.foo"
testInstrumentationRunner "android.test.InstrumentationTestRunner"
}
This is what worked for me only:
androidTestCompile 'net.bytebuddy:byte-buddy-android:0.7.8'
You should use this doc
https://developer.android.com/training/testing/unit-testing/local-unit-tests.html
It describes non-instrumentation unit tests that run on developer machine, not on android device.

Categories

Resources