Skip to content
Android Unit Testing Framework
Java Other
  1. Java 99.3%
  2. Other 0.7%
Branch: master
Clone or download

Latest commit

brettchabot Merge pull request #5680 from robolectric/piper_311487225
Add matcher for expected logs with exception
Latest commit 4764281 Jun 1, 2020

Files

Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.circleci Disable javadoc build on CircleCI master. Mar 10, 2020
.github Update ISSUE_TEMPLATE.md Sep 7, 2017
annotations Move accessibility checking into its own artifact. Mar 26, 2020
buildSrc Support running on Java 13. Mar 26, 2020
errorprone Remove use of about-to-be-deprecated @BugPattern.providesFix. Apr 7, 2020
gradle/wrapper Upgrade to Android Gradle Plugin 3.5.3 and Gradle 5.4.1. Mar 3, 2020
images Add Robolectric logo to README. Jan 7, 2015
integration_tests Update to androidx.test 1.3.0-beta02 May 27, 2020
junit Use @autofactory for building ShadowWranglers. Mar 6, 2020
pluginapi Upgrade to Android Gradle Plugin 3.5.3 and Gradle 5.4.1. Mar 3, 2020
plugins Update to androidx.test 1.3.0-beta02 May 27, 2020
processor Support running on Java 13. Mar 26, 2020
resources Support running on Java 13. Mar 26, 2020
robolectric Add matcher for expected logs with exception May 30, 2020
sandbox Support running on Java 13. Mar 26, 2020
scripts Clean up some logic in the shadows.support.v4 tests Jan 18, 2020
shadowapi Add AndroidInterceptor for System.logW May 11, 2020
shadows Add documentation for setComponentNameAndServiceForBindService and se… May 29, 2020
testapp Remove minSdkVersion from AndroidManifest.xml's. Mar 26, 2020
utils Support running on Java 13. Mar 26, 2020
.gitignore Ignore CTS dirs for now. Jun 9, 2017
CODE_OF_CONDUCT.md Update CODE_OF_CONDUCT.md Feb 26, 2017
LICENSE LICENCE file cleanup. Mar 29, 2017
README.md Update README to reference 4.3.1 Oct 14, 2019
WORKSPACE move workspace file Feb 6, 2018
build.gradle Upgrade to Android Gradle Plugin 3.5.3 and Gradle 5.4.1. Mar 3, 2020
gradle.properties Upgrade to truth 1.0.1 and force annotation processor to use errorpro… Feb 22, 2020
gradlew upgrade gradle to 4.1 Aug 24, 2017
gradlew.bat upgrade gradle to 4.1 Aug 24, 2017
resources.arsc Read binary resources using transliterated Android C++ code (currentl… Mar 23, 2018
settings.gradle Move accessibility checking into its own artifact. Mar 26, 2020

README.md

Build Status GitHub release

Robolectric 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 of an emulator.

Usage

Here's an example of a simple test written using Robolectric:

@RunWith(AndroidJUnit4.class)
public class MyActivityTest {

  @Test
  public void clickingButton_shouldChangeResultsViewText() throws Exception {
    Activity activity = Robolectric.setupActivity(MyActivity.class);

    Button button = (Button) activity.findViewById(R.id.press_me_button);
    TextView results = (TextView) activity.findViewById(R.id.results_text_view);

    button.performClick();
    assertThat(results.getText().toString(), equalTo("Testing Android Rocks!"));
  }
}

For more information about how to install and use Robolectric on your project, extend its functionality, and join the community of contributors, please visit http://robolectric.org.

Install

Starting a New Project

If you'd like to start a new project with Robolectric tests you can refer to deckard (for either maven or gradle) as a guide to setting up both Android and Robolectric on your machine.

build.gradle:

testImplementation "org.robolectric:robolectric:4.3.1"

Building And Contributing

Robolectric is built using Gradle. Both IntelliJ and Android Studio can import the top-level build.gradle file and will automatically generate their project files from it.

Robolectric supports running tests against multiple Android API levels. The work it must do to support each API level is slightly different, so its shadows are built separately for each. To build shadows for every API version, run:

./gradlew clean assemble install compileTest

Using Snapshots

If 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 master and may contain bugs.

build.gradle:

repositories {
    maven { url "https://oss.sonatype.org/content/repositories/snapshots" }
}

dependencies {
    testImplementation "org.robolectric:robolectric:4.4-SNAPSHOT"
}
You can’t perform that action at this time.