xref: /aosp_15_r20/external/robolectric/README.md (revision e6ba16074e6af37d123cb567d575f496bf0a58ee)
1*e6ba1607SAndroid Build Coastguard Worker<a name="README">[<img src="https://rawgithub.com/robolectric/robolectric/master/images/robolectric-horizontal.png"/>](https://robolectric.org)</a>
2*e6ba1607SAndroid Build Coastguard Worker
3*e6ba1607SAndroid Build Coastguard Worker[![Build Status](https://github.com/robolectric/robolectric/actions/workflows/tests.yml/badge.svg)](https://github.com/robolectric/robolectric/actions?query=workflow%3Atests)
4*e6ba1607SAndroid Build Coastguard Worker[![GitHub release](https://img.shields.io/github/release/robolectric/robolectric.svg?maxAge=60)](https://github.com/robolectric/robolectric/releases)
5*e6ba1607SAndroid Build Coastguard Worker
6*e6ba1607SAndroid Build Coastguard WorkerRobolectric is the industry-standard unit testing framework for Android. With Robolectric, your tests run in a simulated Android environment inside a JVM, without the overhead and flakiness of an emulator. Robolectric tests routinely run 10x faster than those on cold-started emulators.
7*e6ba1607SAndroid Build Coastguard Worker
8*e6ba1607SAndroid Build Coastguard WorkerRobolectric supports running unit tests for *14* different versions of Android, ranging from Lollipop (API level 21) to U (API level 34).
9*e6ba1607SAndroid Build Coastguard Worker
10*e6ba1607SAndroid Build Coastguard Worker## Usage
11*e6ba1607SAndroid Build Coastguard Worker
12*e6ba1607SAndroid Build Coastguard WorkerHere's an example of a simple test written using Robolectric:
13*e6ba1607SAndroid Build Coastguard Worker
14*e6ba1607SAndroid Build Coastguard Worker```java
15*e6ba1607SAndroid Build Coastguard Worker@RunWith(AndroidJUnit4.class)
16*e6ba1607SAndroid Build Coastguard Workerpublic class MyActivityTest {
17*e6ba1607SAndroid Build Coastguard Worker
18*e6ba1607SAndroid Build Coastguard Worker  @Test
19*e6ba1607SAndroid Build Coastguard Worker  public void clickingButton_shouldChangeResultsViewText() {
20*e6ba1607SAndroid Build Coastguard Worker    Activity activity = Robolectric.setupActivity(MyActivity.class);
21*e6ba1607SAndroid Build Coastguard Worker
22*e6ba1607SAndroid Build Coastguard Worker    Button button = (Button) activity.findViewById(R.id.press_me_button);
23*e6ba1607SAndroid Build Coastguard Worker    TextView results = (TextView) activity.findViewById(R.id.results_text_view);
24*e6ba1607SAndroid Build Coastguard Worker
25*e6ba1607SAndroid Build Coastguard Worker    button.performClick();
26*e6ba1607SAndroid Build Coastguard Worker    assertThat(results.getText().toString(), equalTo("Testing Android Rocks!"));
27*e6ba1607SAndroid Build Coastguard Worker  }
28*e6ba1607SAndroid Build Coastguard Worker}
29*e6ba1607SAndroid Build Coastguard Worker```
30*e6ba1607SAndroid Build Coastguard Worker
31*e6ba1607SAndroid Build Coastguard WorkerFor more information about how to install and use Robolectric on your project, extend its functionality, and join the community of contributors, please visit [robolectric.org](https://robolectric.org).
32*e6ba1607SAndroid Build Coastguard Worker
33*e6ba1607SAndroid Build Coastguard Worker## Install
34*e6ba1607SAndroid Build Coastguard Worker
35*e6ba1607SAndroid Build Coastguard Worker### Starting a New Project
36*e6ba1607SAndroid Build Coastguard Worker
37*e6ba1607SAndroid Build Coastguard WorkerIf you'd like to start a new project with Robolectric tests, you can refer to `deckard` (for either [Maven](https://github.com/robolectric/deckard-maven) or [Gradle](https://github.com/robolectric/deckard-gradle)) as a guide to setting up both Android and Robolectric on your machine.
38*e6ba1607SAndroid Build Coastguard Worker
39*e6ba1607SAndroid Build Coastguard Worker### `build.gradle`
40*e6ba1607SAndroid Build Coastguard Worker
41*e6ba1607SAndroid Build Coastguard Worker```groovy
42*e6ba1607SAndroid Build Coastguard WorkertestImplementation "junit:junit:4.13.2"
43*e6ba1607SAndroid Build Coastguard WorkertestImplementation "org.robolectric:robolectric:4.13"
44*e6ba1607SAndroid Build Coastguard Worker```
45*e6ba1607SAndroid Build Coastguard Worker
46*e6ba1607SAndroid Build Coastguard Worker## Building and Contributing
47*e6ba1607SAndroid Build Coastguard Worker
48*e6ba1607SAndroid Build Coastguard WorkerRobolectric is built using Gradle. Both Android Studio and IntelliJ can import the top-level `build.gradle.kts` file and will automatically generate their project files from it.
49*e6ba1607SAndroid Build Coastguard Worker
50*e6ba1607SAndroid Build Coastguard WorkerTo get Robolectric up and running on your machine, check out
51*e6ba1607SAndroid Build Coastguard Worker[this guide](https://robolectric.org/building-robolectric/).
52*e6ba1607SAndroid Build Coastguard Worker
53*e6ba1607SAndroid Build Coastguard WorkerTo get a high-level overview of Robolectric's architecture, check out
54*e6ba1607SAndroid Build Coastguard Worker[ARCHITECTURE.md](ARCHITECTURE.md).
55*e6ba1607SAndroid Build Coastguard Worker
56*e6ba1607SAndroid Build Coastguard Worker## Using Snapshots
57*e6ba1607SAndroid Build Coastguard Worker
58*e6ba1607SAndroid Build Coastguard WorkerIf you would like to live on the bleeding edge, you can try running against a snapshot build. Keep in mind that snapshots represent the most recent changes on the `master` and may contain bugs.
59*e6ba1607SAndroid Build Coastguard Worker
60*e6ba1607SAndroid Build Coastguard Worker### `build.gradle`
61*e6ba1607SAndroid Build Coastguard Worker
62*e6ba1607SAndroid Build Coastguard Worker```groovy
63*e6ba1607SAndroid Build Coastguard Workerrepositories {
64*e6ba1607SAndroid Build Coastguard Worker    maven { url "https://oss.sonatype.org/content/repositories/snapshots" }
65*e6ba1607SAndroid Build Coastguard Worker}
66*e6ba1607SAndroid Build Coastguard Worker
67*e6ba1607SAndroid Build Coastguard Workerdependencies {
68*e6ba1607SAndroid Build Coastguard Worker    testImplementation "org.robolectric:robolectric:4.14-SNAPSHOT"
69*e6ba1607SAndroid Build Coastguard Worker}
70*e6ba1607SAndroid Build Coastguard Worker```
71