Used sources:
[1] build.gradle file: http://stackoverflow.com/questions/16649397/robolectric-with-gradle-resources-not-found?rq=1
[2] Add jars to Android Studio: http://stackoverflow.com/questions/16881936/android-studio-missing-external-dependencies
Adapting the build.gradle file according to this link on stackoverflow. build.gradle:
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.5.+'
}
}
repositories {
mavenCentral()
maven {
url 'https://oss.sonatype.org/content/repositories/snapshots/'
}
}
apply plugin: 'android'
android {
compileSdkVersion 17
buildToolsVersion "17.0.0"
defaultConfig {
minSdkVersion 14
targetSdkVersion 16
}
}
sourceSets {
testLocal {
java.srcDir file('src/test/java')
resources.srcDir file('src/test/resources')
}
}
dependencies {
compile 'org.roboguice:roboguice:2.0'
compile 'com.google.android:support-v4:r6'
testLocalCompile 'junit:junit:4.8.2'
testLocalCompile 'org.robolectric:robolectric:2.1'
testLocalCompile 'com.google.android:android:4.0.1.2'
testLocalCompile 'com.google.android:support-v4:r6'
testLocalCompile 'org.roboguice:roboguice:2.0'
}
task localTest(type: Test, dependsOn: assemble) {
testClassesDir = sourceSets.testLocal.output.classesDir
android.sourceSets.main.java.srcDirs.each { dir ->
def buildDir = dir.getAbsolutePath().split('/')
buildDir = (buildDir[0..(buildDir.length - 4)] + ['build', 'classes', 'debug']).join('/')
sourceSets.testLocal.compileClasspath += files(buildDir)
sourceSets.testLocal.runtimeClasspath += files(buildDir)
}
classpath = sourceSets.testLocal.runtimeClasspath
}
check.dependsOn localTest
This results in a working build using this DummyTest class in src/test/java/...
package org.fhict.calculator.rtest;
import junit.framework.Assert;
import roboguice.test.RobolectricRoboTestRunner;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;
import org.junit.runner.RunWith;
import org.junit.Before;
import org.junit.Test;
import org.robolectric.Robolectric;
import org.robolectric.RobolectricTestRunner;
@RunWith(RobolectricTestRunner.class)
public class DummyTest {
@Before
public void setUp() {
}
@Test
public void testSomeNumber() {
Assert.assertEquals(true,true);
}
}
package org.fhict.calculator.rtest;
import junit.framework.Assert;
import roboguice.test.RobolectricRoboTestRunner;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;
import org.junit.runner.RunWith;
import org.junit.Before;
import org.junit.Test;
import org.robolectric.Robolectric;
import org.robolectric.RobolectricTestRunner;
@RunWith(RobolectricTestRunner.class)
public class DummyTest {
@Before
public void setUp() {
}
@Test
public void testSomeNumber() {
Assert.assertEquals(true,true);
}
}
From command line a build is now running ('gradle localTest' from terminal). Generating a test-report in file:///home/ben/AndroidStudioProjects/CalculatorProject/Calculator/build/reports/tests/org.fhict.calculator.rtest.DummyTest.html
AndroidStudio does not yet recognize the new jar files as defined by testLocalCompile in build.gradle. Manually add them to AndroidStudio using this link.
Remaining open issues:
(1) How to show the output of the Robolectric unit tests in AndroidStudio?
(2) How to force recompile when the RoboLectric unit test has been changed? Now only a gradle clean && gradle localTest. Not so nice...
Please comment if you know how to solve these?
Please comment if you know how to solve these?