Roboelectric and Android Studio

I tried to do this for several days, with no results. I need to configure Robolectric in Android Studio (0.8.9, latest version). I followed various tutorials on Android testing and integration , Roboelectric installation for unit testing , Android Gradle application with Roboelectric , How to run Roboelectric JUnit tests , but always got some kind of error.

So, I created a module specifically for testing:

enter image description here

enter image description here

Kedzoh (Project) build.gradle :

// Top-level build file where you can add configuration options common to all sub- projects/modules. buildscript { repositories { jcenter() } dependencies { classpath 'com.android.tools.build:gradle:0.12.+' classpath 'org.robolectric:robolectric-gradle-plugin:0.12.+' } } allprojects { repositories { jcenter() } } 

build.gradle application:

 apply plugin: 'com.android.application' apply plugin: 'android' android { compileSdkVersion 19 buildToolsVersion '19.1.0' defaultConfig { applicationId 'com.dev.kedzoh' minSdkVersion 9 targetSdkVersion 19 versionCode 14 versionName '1.6.7' } buildTypes { release { runProguard false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } productFlavors { } compileOptions { sourceCompatibility JavaVersion.VERSION_1_7 targetCompatibility JavaVersion.VERSION_1_7 } } dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) compile 'com.android.support:appcompat-v7:19.+' compile 'com.google.android.gms:play-services:4.2.42' compile 'com.squareup.retrofit:retrofit:1.6.1' compile 'com.squareup.okhttp:okhttp:2.0.0' compile 'com.squareup.okhttp:okhttp-urlconnection:2.0.0' compile 'com.google.code.gson:gson:2.2.4' compile 'com.squareup.picasso:picasso:2.3.3' // You must install or update the Support Repository through the SDK manager to use this dependency. compile 'com.android.support:support-v4:19.+' compile 'com.google.guava:guava:18.0-rc1' compile 'com.sothree.slidinguppanel:library:+' compile 'com.larswerkman:HoloColorPicker:1.4' } 

kedzoh-tests build.gradle:

 apply plugin: 'java' dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) compile 'com.squareup.retrofit:retrofit:1.6.1' compile 'com.squareup.okhttp:okhttp:2.0.0' compile 'com.squareup.okhttp:okhttp-urlconnection:2.0.0' compile 'com.google.code.gson:gson:2.2.4' compile 'com.squareup.picasso:picasso:2.3.3' // You must install or update the Support Repository through the SDK manager to use this dependency. compile 'com.google.guava:guava:18.0-rc1' compile 'com.sothree.slidinguppanel:library:+' compile 'com.larswerkman:HoloColorPicker:1.4' testCompile 'junit:junit:4.+' testCompile 'org.robolectric:robolectric:2.2' } 

At this moment I cannot import Robolectric classes, this gives me an error. When I add the application plugin: "robolectric" to kedzoh-tests build.gradle , it requests the "android" plugin. After I add, he complains that there is no manifest and cannot be built. I am not sure if this is the correct configuration since it never worked. Can someone give some advice on how to install Robolectric in Android Studio, please?

EDIT:

I tried the answer below, but still stuck with the "Class not found" error:

enter image description here

enter image description here

enter image description here

+8
android android-studio gradle robolectric
source share
2 answers

There are already different project templates for robolectric. Have you tried https://github.com/nenick/android-gradle-template ?

+2
source share

I think that you can complicate and complicate your life by creating your test module outside the main module "application". Most of the tutorials I've seen have an androidTest folder in the application module, which may contain your Robolectric tests. All the old Eclipse-based tutorials that I saw seem to teach us how to create test modules separately from the main project.

If I used Robolectric, I would set up the project as I created for you https://github.com/marcthomas2013/Kedzoh . I would use the androidTest folder to put my tests and configure gradle files as follows.

Note that dependencies for tests are included using androidTestCompile declarations, and application dependencies are declared using compilation.

Another element in this gradle file is the robolectric section, where it includes and excludes classes in the androidTest folder. Everything that is not in the espresso folder will be launched using the target test program gradlew, and everything, including the espresso folder, will be launched when the target function connectledAndroidTest is called.

 app gradle file apply plugin: 'com.android.application' apply plugin: 'robolectric' android { packagingOptions { exclude 'LICENSE.txt' exclude 'META-INF/LICENSE' exclude 'META-INF/LICENSE.txt' exclude 'META-INF/NOTICE' } compileSdkVersion 19 buildToolsVersion "19.1.0" defaultConfig { minSdkVersion 18 targetSdkVersion 18 versionCode 2 versionName "1.0.0-SNAPSHOT" testInstrumentationRunner "com.google.android.apps.common.testing.testrunner.GoogleInstrumentationTestRunner" } buildTypes { release { runProguard false } } sourceSets { androidTest { setRoot('src/androidTest') } } } robolectric { include '**/*Test.class' exclude '**/espresso/**/*.class' } dependencies { repositories { mavenCentral() } compile 'com.sothree.slidinguppanel:library:2.0.1' androidTestCompile('junit:junit:4.11') { exclude module: 'hamcrest-core' } // Espresso androidTestCompile files('libs/espresso-1.1.jar') androidTestCompile files('libs/testrunner-1.1.jar') androidTestCompile files('libs/testrunner-runtime-1.1.jar') androidTestCompile 'com.google.guava:guava:14.0.1' androidTestCompile 'com.squareup.dagger:dagger:1.1.0' androidTestCompile 'org.hamcrest:hamcrest-integration:1.1' androidTestCompile 'org.hamcrest:hamcrest-core:1.1' androidTestCompile 'org.hamcrest:hamcrest-library:1.1' androidTestCompile('org.robolectric:robolectric:2.3') { exclude module: 'classworlds' exclude module: 'commons-logging' exclude module: 'httpclient' exclude module: 'maven-artifact' exclude module: 'maven-artifact-manager' exclude module: 'maven-error-diagnostics' exclude module: 'maven-model' exclude module: 'maven-project' exclude module: 'maven-settings' exclude module: 'plexus-container-default' exclude module: 'plexus-interpolation' exclude module: 'plexus-utils' exclude module: 'wagon-file' exclude module: 'wagon-http-lightweight' exclude module: 'wagon-provider-api' } androidTestCompile 'com.squareup:fest-android:1.0.+' } 

project build file Here we include the robolectric class path so that we can use the robolectric gradle commands in the configuration file above.

 // Top-level build file where you can add configuration options common to all sub-projects/modules. buildscript { repositories { jcenter() mavenCentral() } dependencies { classpath 'com.android.tools.build:gradle:0.12.2' classpath 'org.robolectric:robolectric-gradle-plugin:0.11.+' // NOTE: Do not place your application dependencies here; they belong // in the individual module build.gradle files } } allprojects { repositories { jcenter() mavenCentral() } } 

Can you explain what mistakes you have, as there may be many possible errors based on how you work, there is also a special reason for creating a separate test module.

The project that I provided is based on setting up a gradle deck ( https://github.com/robolectric/deckard-gradle ), there are some tips in this about JUnit conflicts, etc. that you may encounter.

Once you get so far as to be able to debug unit test in the application, you will have to manually configure the class path when starting the test (not really at all!), Based on the information in this article https://github.com/codepath/android_guides / wiki / Robolectric-Installation-for-Unit-Testing

You will need to run the unit test, wait for it to fail, copy the -classpath parameter and its value (it will be quite long and start and end with ") and copy it to your virtual machine parameters in your configuration run, and then at the end of this class path add a or a: depending on your OS path route and add a path to your test classes, which will be something like <ABSOLUTE_PATH_TO_PROJECT>/build/test-classes , this will add your test classes to the class path, and then Android Studio should be able to run them.

It also suggests that the steps to run gradle testClasses were made from http://blog.blundell-apps.com/how-to-run-robolectric-junit-tests-in-android-studio/

0
source share

All Articles