or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

component-lifecycle.mdindex.mdlooper-threading.mdruntime-environment.mdshadow-system.mdtest-runner.md
tile.json

tessl/maven-org-robolectric--robolectric

The industry-standard unit testing framework for Android that enables running tests in a simulated Android environment inside a JVM without requiring an emulator or device.

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
mavenpkg:maven/org.robolectric/robolectric@4.15.x

To install, run

npx @tessl/cli install tessl/maven-org-robolectric--robolectric@4.15.0

index.mddocs/

Robolectric

Robolectric is the industry-standard unit testing framework for Android that enables running tests in a simulated Android environment inside a JVM without requiring an emulator or device. It provides comprehensive Android API mocking, lifecycle management, and shadow implementations for 15 different Android versions (API levels 21-35), making tests run 10x faster than emulator-based tests.

Package Information

  • Package Name: org.robolectric:robolectric
  • Package Type: maven
  • Language: Java
  • Installation: Add to your build.gradle:
testImplementation "org.robolectric:robolectric:4.15.1"

Core Imports

import org.robolectric.Robolectric;
import org.robolectric.RuntimeEnvironment;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.annotation.Config;
import org.robolectric.annotation.LooperMode;

For component controllers:

import org.robolectric.android.controller.ActivityController;
import org.robolectric.android.controller.ServiceController;

For shadow access:

import static org.robolectric.Shadows.shadowOf;

Basic Usage

import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.Robolectric;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.annotation.Config;

@RunWith(RobolectricTestRunner.class)
@Config(sdk = 28)
public class MyActivityTest {

    @Test
    public void testActivityCreation() {
        // Create and setup activity
        MyActivity activity = Robolectric.setupActivity(MyActivity.class);
        
        // Test activity behavior
        assertThat(activity).isNotNull();
        assertThat(activity.isFinishing()).isFalse();
    }

    @Test
    public void testButtonClick() {
        // Build activity with controller for lifecycle management
        ActivityController<MyActivity> controller = 
            Robolectric.buildActivity(MyActivity.class);
        MyActivity activity = controller.create().start().resume().get();

        // Find views and test interactions
        Button button = activity.findViewById(R.id.my_button);
        TextView textView = activity.findViewById(R.id.my_text);
        
        button.performClick();
        
        assertThat(textView.getText().toString()).isEqualTo("Button clicked!");
        
        // Clean up
        controller.pause().stop().destroy();
    }
}

Architecture

Robolectric's architecture consists of several key components:

  • Test Runner: RobolectricTestRunner provides the JUnit integration and test environment setup
  • Component Controllers: Manage Android component lifecycles (Activity, Service, etc.)
  • Shadow System: Comprehensive mock implementations of Android framework classes
  • Runtime Environment: Simulates Android runtime with configurable device properties
  • Configuration System: Annotations for controlling test behavior and Android simulation
  • Sandboxing: Bytecode instrumentation for Android API interception and redirection

Capabilities

Test Runner and Configuration

Core test execution framework with comprehensive configuration options for SDK levels, device properties, application settings, and test behavior control.

@RunWith(RobolectricTestRunner.class)
public class RobolectricTestRunner extends SandboxTestRunner {
    // JUnit test runner implementation
}

@Config(
    sdk = {28, 29, 30},
    application = MyApplication.class,
    qualifiers = "xlarge-land-mdpi"
)
public @interface Config {
    // Configuration annotation
}

Test Runner and Configuration

Component Lifecycle Management

Factory methods and controller classes for managing Android component lifecycles including Activities, Services, ContentProviders, and Fragments with full lifecycle control.

public class Robolectric {
    public static <T extends Activity> ActivityController<T> buildActivity(Class<T> activityClass);
    public static <T extends Activity> ActivityController<T> buildActivity(Class<T> activityClass, Intent intent);
    public static <T extends Service> ServiceController<T> buildService(Class<T> serviceClass);
    public static <T extends ContentProvider> ContentProviderController<T> buildContentProvider(Class<T> contentProviderClass);
}

Component Lifecycle Management

Runtime Environment Control

Runtime environment management for accessing the Application context, controlling device configuration, managing threading, and manipulating system properties.

public class RuntimeEnvironment {
    public static Application getApplication();
    public static void setQualifiers(String newQualifiers);
    public static void setFontScale(float fontScale);
    public static int getApiLevel();
    public static String getQualifiers();
}

Runtime Environment Control

Shadow System

Access to shadow implementations of Android framework classes that provide controllable mock behavior for testing, with the ability to extract shadow objects and control their behavior.

public class Shadows {
    public static <T> T shadowOf(Object instance);
    // Generated methods for each Android class:
    // public static ShadowActivity shadowOf(Activity activity);
    // public static ShadowApplication shadowOf(Application application);
    // public static ShadowView shadowOf(View view);
}

Shadow System

Looper and Threading Control

Comprehensive control over Android's Looper and threading behavior with multiple modes for different testing scenarios, from legacy scheduler-based control to realistic paused execution.

@LooperMode(LooperMode.Mode.PAUSED)
public @interface LooperMode {
    enum Mode {
        LEGACY,    // @Deprecated scheduler-based control
        PAUSED,    // Realistic paused execution (recommended)
        INSTRUMENTATION_TEST  // Separate test thread
    }
}

Looper and Threading Control