-
Notifications
You must be signed in to change notification settings - Fork 6.3k
Robolectric Installation for Unit Testing
Robolectric is a framework that allows Android applications to be tested on the JVM using JUnit 4 (no emulator or device needed!). More info about robolectric is available here.
In this guide, we'll walk through the steps necessary to add Robolectric to an existing project.
- Android Studio 1.2.1.1+
- Android Gradle Plugin 1.2.3+
- Gradle 2.2.1+
Note: Robolectric can also be configured with Android Studio 1.1, but the setup requires the robolectric gradle plugin and some additional configuration. Also, keep in mind that unit testing was still considered an experimental feature in Android Studio 1.1.
First, we need to add the following to the app build.gradle. Robolectric's latest version is currently 3.0
.
dependencies {
...
testCompile 'org.robolectric:robolectric:3.0'
}
Next, there are a couple things we need to configure in Android Studio for running unit tests. One very important setting is the Test Artifact
setting that toggles between Unit Tests
and Android Instrumentation Tests
. This setting controls the visibility of the corresponding tests in the project browser.
For each type of test, Android Studio defaults to looking for tests in the following locations:
- Unit Tests =>
src/test/java
- Android Instrumentation Tests =>
src/androidTest/java
Since we will be creating unit tests, we need our robolectric tests to live in src/test/java
. The easiest way to create this folder (if instrumentations tests aren't also needed) is to rename the androidTest
folder to test
like so:
Note: If you are using a Mac, there is a known issue that needs to be addressed so that tests can be located properly. Go to Run
-> Edit Configurations
-> Defaults
-> Junit
and make sure to set $MODULE_DIR$
as the working directory. You can see the Robolectric getting started guide for more information.
That's all the setup needed. Now we can move on to writing a test.
The code below shows a basic Robolectric test that verifies the expected text inside of a TextView. To enable this test:
- Make sure you have a test file named
MainActivityTest.java
(you can renameApplicationTest.java
toMainActivityTest.java
if you'd like) - Make sure your app has an Activity named
MainActivity
- Add a TextView to
MainActivity
namedtvHello
containing the text "Hello world!" - Copy the code below into
MainActivityTest.java
import android.widget.TextView;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.Robolectric;
import org.robolectric.RobolectricGradleTestRunner;
import org.robolectric.annotation.Config;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
@Config(constants = BuildConfig.class, sdk = 21)
@RunWith(RobolectricGradleTestRunner.class)
public class MainActivityTest {
private MainActivity activity;
@Before
public void setup() throws Exception {
activity = Robolectric.setupActivity(MainActivity.class);
assertNotNull("MainActivity is not instantiated", activity);
}
@Test
public void validateTextViewContent() throws Exception {
TextView tvHelloWorld = (TextView) activity.findViewById(R.id.tvHello);
assertNotNull("TextView could not be found", tvHelloWorld);
assertTrue("TextView contains incorrect text",
"Hello world!".equals(tvHelloWorld.getText().toString()));
}
}
Note: Robolectric currently doesn't support API level 22 (Android 5.1). If your app targets API 22, you will need to specify the sdk = 21
annotation.
- Android Studio - Make sure you run the test as a Gradle test (the first item). The first item is for a Gradle test, while the 2nd item is for JUnit.
If all goes well, you will see the passing results in the console. Note: you may need to enable Show Passed
as in the diagram to see the full results.
To run tests on the command line ./gradlew test
Created by CodePath with much help from the community. Contributed content licensed under cc-wiki with attribution required. You are free to remix and reuse, as long as you attribute and use a similar license.
Finding these guides helpful?
We need help from the broader community to improve these guides, add new topics and keep the topics up-to-date. See our contribution guidelines here and our topic issues list for great ways to help out.
Check these same guides through our standalone viewer for a better browsing experience and an improved search. Follow us on twitter @codepath for access to more useful Android development resources.