Robolectric works best with Gradle or Maven. If you are starting a new project, we would reccomend Gradle first (since it is the build system of choice in Android Studio) and Maven second. Regardless, Robolectric should integrate well into whatever build tool you are using.
Building with Gradle
Add the following to your build.gradle:
testCompile "org.robolectric:robolectric:3.0"
Annotate your test with the Gradle test runner:
@RunWith(RobolectricGradleTestRunner.class)
@Config(constants = BuildConfig.class)
public class SandwichTest {
}
Note that you must specify the constants field which points to the BuildConfig.class generated by the build system. Robolectric uses the constants in the class to compute the output paths used by Gradle when building your project. Without these values, Robolectric will not be able to find your merged manifest, resources, or assets.
Building with Maven
Add the following to your pom.xml:
<dependency>
<groupId>org.robolectric</groupId>
<artifactId>robolectric</artifactId>
<version>3.0</version>
<scope>test</scope>
</dependency>
Annotate your test with the base test runner:
@RunWith(RobolectricTestRunner.class)
public class SandwichTest {
}
If you reference resources that are outside of your project (i.e. in a aar dependency), you will need to provide Robolectric with a pointer to the exploded aar in your build system. See the section titled "Using Library Resources" for more information.
Building with Android Studio
Robolectric works with Android Studio 1.1.0 or newer. Simply follow the instructions above for working with Gradle. Enable the unit test support in the "Build Variants" tab and run your test.
Comments
New External Articles view