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/