Download Android Studio 1.1
Download and install Android Studio for Mac.
Setup Unit Testing Support in Android Studio
Check Enable Unit Testing support under Gradle > Experimental in the Android Studio preferences.
Open up Build Variants and set the Test Artifact to Unit Tests.
Create a test directory.
Setup the Robolectric Gradle Plugin
Add the Robolectric Gradle plugin to the top-level build file.
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:1.1.0'
classpath 'org.robolectric:robolectric-gradle-plugin:1.0.1'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
jcenter()
}
}
Apply the Robolectric Gradle plugin and add its dependencies in the app module build file.
app/build.gradle
apply plugin: 'com.android.application'
android {
compileSdkVersion 21
buildToolsVersion "21.1.2"
defaultConfig {
applicationId "com.example.pivotal.helloworld"
minSdkVersion 21
targetSdkVersion 21
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
apply plugin: 'org.robolectric'
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:support-v4:21.0.3' // required by Robolectric 2.4
testCompile 'junit:junit:4.12'
testCompile 'org.robolectric:robolectric:2.4'
}
Write a Robolectric Test
app/src/test/java/com/example/pivotal/helloworld/HelloActivityTest.java
package com.example.pivotal.helloworld;
import android.app.AlertDialog;
import android.widget.Button;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.Robolectric;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.annotation.Config;
import org.robolectric.shadows.ShadowAlertDialog;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.notNullValue;
import static org.junit.Assert.assertThat;
@RunWith(RobolectricTestRunner.class)
@Config(manifest = "app/src/main/AndroidManifest.xml", emulateSdk = 18)
public class HelloActivityTest {
@Test
public void buttonTapDisplaysAnAlertDialog() {
HelloActivity helloActivity = Robolectric.setupActivity(HelloActivity.class);
Button button = (Button) helloActivity.findViewById(R.id.hi);
Robolectric.clickOn(button);
AlertDialog latestAlertDialog = ShadowAlertDialog.getLatestAlertDialog();
assertThat(latestAlertDialog, is(notNullValue()));
ShadowAlertDialog shadowAlertDialog = Robolectric.shadowOf(latestAlertDialog);
assertThat(shadowAlertDialog.getTitle().toString(), is("Hi"));
}
}
Add code to pass the test.
app/src/main/java/com/example/pivotal/helloworld/HelloActivity.java
package com.example.pivotal.helloworld;
import android.app.Activity;
import android.app.AlertDialog;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class HelloActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_hello);
Button button = (Button) findViewById(R.id.hi);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
new AlertDialog.Builder(HelloActivity.this)
.setTitle("Hi")
.create()
.show();
}
});
}
}
app/src/main/java/res/layout/activity_hello.xml
<?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <Button android:id="@+id/hi" android:text="Hi" android:layout_width="wrap_content" android:layout_height="wrap_content"/> </FrameLayout>
About the Author