-
Open your top level
build.gradle
file. -
Add Sonatype repo to buildscript repositories and Square Gradle Android Test Plugin to buildscript dependencies. (Older Gradle projects may automatically put this buildscript code in your project build.gradle, in which situation you should put the following in that file.)
buildscript {
repositories {
mavenCentral()
maven {
url 'https://oss.sonatype.org/content/repositories/snapshots/'
}
}
dependencies {
classpath 'com.android.tools.build:gradle:0.7.+'
classpath 'com.squareup.gradle:gradle-android-test-plugin:0.9.1-SNAPSHOT'
}
}
-
Open your project
build.gradle
file. -
Add Android Test Plugin.
apply plugin: 'android'
apply plugin: 'android-test'
- Add Sonatype repo to project repositories.
repositories {
mavenCentral()
maven {
url 'https://oss.sonatype.org/content/repositories/snapshots/'
}
}
- Add the following to your project dependancies.
dependencies {
...
testCompile 'junit:junit:4.10'
testCompile 'org.robolectric:robolectric:2.1.+'
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.+'
}
- Create test source folders.
/src/test/java
/src/test/res
- Specify test configuration in android options in the project build.gradle.
android {
compileSdkVersion 19
buildToolsVersion "19.0.1"
defaultConfig {
minSdkVersion 13
targetSdkVersion 19
versionCode 1
versionName "1.0"
testPackageName "com.bnotions.robolectricgradletest.test"
testInstrumentationRunner "android.test.InstrumentationTestRunner"
}
buildTypes {
release {
runProguard false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
}
}
sourceSets {
instrumentTest.setRoot('src/test')
}
}
-
Sync project with Gradle files.
-
Rebuild.
-
Create package
com.yourcompany.yourproject.test
inside the /src/test/java folder. -
Add the following class to it.
public class RobolectricGradleTestRunner extends RobolectricTestRunner {
public RobolectricGradleTestRunner(Class<?> testClass) throws InitializationError {
super(testClass);
}
@Override
protected AndroidManifest getAppManifest(Config config) {
String manifestProperty = System.getProperty("android.manifest");
if (config.manifest().equals(Config.DEFAULT) && manifestProperty != null) {
String resProperty = System.getProperty("android.resources");
String assetsProperty = System.getProperty("android.assets");
return new AndroidManifest(Fs.fileFromPath(manifestProperty), Fs.fileFromPath(resProperty),
Fs.fileFromPath(assetsProperty));
}
AndroidManifest appManifest = super.getAppManifest(config);
return appManifest;
}
}
- Create your first test. All test files must specify the following @RunWith annotation on the class.
@RunWith(RobolectricGradleTestRunner.class)
public class MainActivityTest {
@Test
public void userInterfaceTest() {
Activity activity = Robolectric.buildActivity(MainActivity.class).create().get();
TextView tvHello = (TextView) activity.findViewById(R.id.tvHello);
String resultsText = tvHello.getText().toString();
Assert.assertEquals(
Robolectric.application.getResources().getString(R.string.hello_world),
resultsText);
}
}
-
Run your tests from the command line by calling
./gradlew test
-
Your test report can be found at
/<Project>/build/test-report/index.html