Android Studio: 1.2
proejct/app/build.gradle
1. Add class path to project build.
proejct/buiild.gradle
classpath 'org.robolectric:robolectric-gradle-plugin:1.0.1'
2. Add robolectric plugin to app build.
proejct/app/build.gradleproject/app/build.gradle
apply plugin: 'org.robolectric'
3. Add dependencies to app build.
proejct/app/build.gradle
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:22.1.1'
testCompile 'junit:junit:4.12'
testCompile 'org.robolectric:robolectric:2.4'
}
4. Add configurations for convenience to app build.
proejct/app/build.gradle
robolectric {
// Configure includes / excludes
include '**/*Test.class'
exclude '**/espresso/**/*.class'
// Configure max heap size of the test JVM
maxHeapSize = '2048m'
// Configure the test JVM arguments - Does not apply to Java 8
jvmArgs '-XX:MaxPermSize=512m', '-XX:-UseSplitVerifier'
// Specify max number of processes (default is 1)
maxParallelForks = 4
// Specify max number of test classes to execute in a test process
// before restarting the process (default is unlimited)
forkEvery = 150
// configure whether failing tests should fail the build
ignoreFailures true
// use afterTest to listen to the test execution results
afterTest { descriptor, result ->
println "Executing test for ${descriptor.name} with result: ${result.resultType}"
}
}
Your gradle build files should look like below:
proejct/build.gradle
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:1.2.2'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
classpath 'org.robolectric:robolectric-gradle-plugin:1.0.1'
}
}
allprojects {
repositories {
jcenter()
}
}
proejct/app/build.gradle
apply plugin: 'com.android.application'
apply plugin: 'org.robolectric'
android {
compileSdkVersion 22
buildToolsVersion "22.0.1"
defaultConfig {
applicationId "com.spark.eample.robolectrictest"
minSdkVersion 10
targetSdkVersion 22
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:22.1.1'
testCompile 'junit:junit:4.12'
testCompile 'org.robolectric:robolectric:2.4'
}
robolectric {
// Configure includes / excludes
include '**/*Test.class'
exclude '**/espresso/**/*.class'
// Configure max heap size of the test JVM
maxHeapSize = '2048m'
// Configure the test JVM arguments - Does not apply to Java 8
jvmArgs '-XX:MaxPermSize=512m', '-XX:-UseSplitVerifier'
// Specify max number of processes (default is 1)
maxParallelForks = 4
// Specify max number of test classes to execute in a test process
// before restarting the process (default is unlimited)
forkEvery = 150
// configure whether failing tests should fail the build
ignoreFailures true
// use afterTest to listen to the test execution results
afterTest { descriptor, result ->
println "Executing test for ${descriptor.name} with result: ${result.resultType}"
}
}
5. Config Build Variants.
You can find Build Variants panel on the left bottom in your Android Studio. Otherwise, click View>Tool Windows>Build Variants in the main menu. And then change Test Artifact to Unit Tests to use JUnit as default test framework.
6. Write a test code.
Create test folder app/src/textDebug/java. This would be your main test directory and of course, you can create packages under this directory. Here is a sample test code.
package com.spark.eample.robolectrictest;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.Robolectric;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.annotation.Config;
import static org.hamcrest.core.Is.is;
import static org.junit.Assert.assertThat;
@Config(emulateSdk = 18)
@RunWith(RobolectricTestRunner.class)
public class MainModelTest {
@Before
public void setUp() throws Exception {
//Do something for initialisation
}
@Test
public void testGetText() throws Exception {
assertThat(3+4, is(7));
}
}
7. Run the test.
Right click on the testGetText() method and click Run 'testGetText'. You will see the test result in the Run panel on the bottom.
Comments
Post a Comment