CtrlK
BlogDocsLog inGet started
Tessl Logo

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.

Pending
Overview
Eval results
Files

component-lifecycle.mddocs/

Component Lifecycle Management

Factory methods and controller classes for managing Android component lifecycles with precise control over creation, lifecycle transitions, and cleanup.

Capabilities

Activity Management

Factory methods and controller for managing Activity lifecycle with full control over creation, lifecycle states, and configuration changes.

public class Robolectric {
    /**
     * Creates ActivityController for the given activity class.
     * Consider using androidx.test.core.app.ActivityScenario instead for higher-level APIs.
     */
    public static <T extends Activity> ActivityController<T> buildActivity(Class<T> activityClass);
    
    /**
     * Creates ActivityController with intent. Note: activity class is not determined by intent.
     * Consider using androidx.test.core.app.ActivityScenario instead for higher-level APIs.
     */
    public static <T extends Activity> ActivityController<T> buildActivity(Class<T> activityClass, Intent intent);
    
    /**
     * Creates ActivityController with intent and activity options.
     * Only Display ID is currently supported in options bundle.
     * Consider using androidx.test.core.app.ActivityScenario instead for higher-level APIs.
     */
    public static <T extends Activity> ActivityController<T> buildActivity(
        Class<T> activityClass, 
        Intent intent, 
        Bundle activityOptions);
    
    /**
     * @deprecated Use androidx.test.core.app.ActivityScenario instead
     * Simulates starting activity and returns its reference.
     */
    @Deprecated
    public static <T extends Activity> T setupActivity(Class<T> activityClass);
}

ActivityController<T>

Low-level controller providing precise Activity lifecycle management. Implements AutoCloseable for resource cleanup.

/**
 * ActivityController provides low-level APIs to control activity's lifecycle.
 * Using ActivityController directly is discouraged - use ActivityScenario instead.
 */
public class ActivityController<T extends Activity> 
    extends ComponentController<ActivityController<T>, T> 
    implements AutoCloseable {
    
    // Lifecycle state management
    public ActivityController<T> create();
    public ActivityController<T> create(Bundle savedInstanceState);
    public ActivityController<T> postCreate(Bundle savedInstanceState);
    public ActivityController<T> start();
    public ActivityController<T> restoreInstanceState(Bundle savedInstanceState);
    public ActivityController<T> resume();
    public ActivityController<T> postResume();
    public ActivityController<T> visible();
    public ActivityController<T> windowFocusChanged(boolean hasFocus);
    public ActivityController<T> userLeaving();
    public ActivityController<T> pause();
    public ActivityController<T> saveInstanceState(Bundle outState);
    public ActivityController<T> stop();
    public ActivityController<T> restart();
    public ActivityController<T> destroy();
    
    // Convenience methods
    public ActivityController<T> setup();  // create().start().postCreate().resume().visible()
    public T get();  // Returns the Activity instance
    
    // Configuration and intent handling
    public ActivityController<T> configurationChange(Configuration newConfiguration);
    public ActivityController<T> newIntent(Intent intent);
    public ActivityController<T> recreate();
    
    // AutoCloseable implementation
    public void close();
}

Usage Example:

// Basic lifecycle control
ActivityController<MyActivity> controller = Robolectric.buildActivity(MyActivity.class);
MyActivity activity = controller.create().start().resume().get();

// Test activity state
assertThat(activity.isFinishing()).isFalse();

// Simulate configuration change
Configuration newConfig = new Configuration();
newConfig.orientation = Configuration.ORIENTATION_LANDSCAPE;
controller.configurationChange(newConfig);

// Complete lifecycle
controller.pause().stop().destroy();

// Or use try-with-resources for automatic cleanup
try (ActivityController<MyActivity> controller = Robolectric.buildActivity(MyActivity.class)) {
    MyActivity activity = controller.setup().get();
    // Test activity
} // Automatically calls controller.close()

Service Management

Factory methods and controller for managing Service and IntentService lifecycles.

public class Robolectric {
    /** Creates ServiceController for the given service class */
    public static <T extends Service> ServiceController<T> buildService(Class<T> serviceClass);
    
    /** Creates ServiceController with intent */
    public static <T extends Service> ServiceController<T> buildService(Class<T> serviceClass, Intent intent);
    
    /** Creates and sets up service (calls create()) */
    public static <T extends Service> T setupService(Class<T> serviceClass);
    
    /** Creates IntentServiceController for the given service class */
    public static <T extends IntentService> IntentServiceController<T> buildIntentService(Class<T> serviceClass);
    
    /** Creates IntentServiceController with intent */
    public static <T extends IntentService> IntentServiceController<T> buildIntentService(Class<T> serviceClass, Intent intent);
    
    /** Creates and sets up intent service */
    public static <T extends IntentService> T setupIntentService(Class<T> serviceClass);
}

ServiceController<T>

Controller for managing Service lifecycle including binding and command handling.

/**
 * Controller for managing Service lifecycle.
 */
public class ServiceController<T extends Service> 
    extends ComponentController<ServiceController<T>, T> {
    
    // Basic lifecycle
    public ServiceController<T> create();
    public ServiceController<T> destroy();
    public T get();
    
    // Binding lifecycle  
    public ServiceController<T> bind();
    public ServiceController<T> unbind();
    public ServiceController<T> rebind();
    
    // Command handling
    public ServiceController<T> startCommand(int flags, int startId);
    public ServiceController<T> startCommand(Intent intent, int flags, int startId);
}

IntentServiceController<T>

Specialized controller for IntentService lifecycle management.

/**
 * Controller for managing IntentService lifecycle.
 */
public class IntentServiceController<T extends IntentService> 
    extends ComponentController<IntentServiceController<T>, T> {
    
    public IntentServiceController<T> create();
    public IntentServiceController<T> destroy();  
    public T get();
    
    // IntentService automatically handles intents in background thread
}

Usage Example:

// Service lifecycle
ServiceController<MyService> controller = Robolectric.buildService(MyService.class);
MyService service = controller.create().get();

// Test service startup
assertThat(service).isNotNull();

// Simulate binding
IBinder binder = controller.bind().get().onBind(new Intent());
assertThat(binder).isNotNull();

// Start command
Intent intent = new Intent("my.action");
controller.startCommand(intent, 0, 1);

// Cleanup
controller.unbind().destroy();

ContentProvider Management

Factory methods and controller for ContentProvider lifecycle management.

public class Robolectric {
    /** Creates ContentProviderController for the given provider class */
    public static <T extends ContentProvider> ContentProviderController<T> buildContentProvider(Class<T> contentProviderClass);
    
    /** Creates and sets up content provider */
    public static <T extends ContentProvider> T setupContentProvider(Class<T> contentProviderClass);
    
    /** Creates and sets up content provider with authority */
    public static <T extends ContentProvider> T setupContentProvider(Class<T> contentProviderClass, String authority);
}

ContentProviderController<T>

Controller for managing ContentProvider lifecycle with authority configuration.

/**
 * Controller for managing ContentProvider lifecycle.
 */
public class ContentProviderController<T extends ContentProvider> 
    extends ComponentController<ContentProviderController<T>, T> {
    
    public ContentProviderController<T> create();
    public ContentProviderController<T> create(String authority);
    public ContentProviderController<T> shutdown();
    public T get();
}

Fragment Management (Deprecated)

Factory methods for Fragment controllers. Use androidx FragmentScenario instead.

public class Robolectric {
    /**
     * @deprecated Native Fragments deprecated in Android P. Use androidx.fragment.app.testing.FragmentScenario
     */
    @Deprecated
    public static <T extends Fragment> FragmentController<T> buildFragment(Class<T> fragmentClass);
    
    /** 
     * @deprecated Use FragmentScenario instead
     */
    @Deprecated  
    public static <T extends Fragment> FragmentController<T> buildFragment(Class<T> fragmentClass, Bundle arguments);
    
    /**
     * @deprecated Bad practice to depend on specific activity. Use FragmentScenario instead
     */
    @Deprecated
    public static <T extends Fragment> FragmentController<T> buildFragment(
        Class<T> fragmentClass, 
        Class<? extends Activity> activityClass);
    
    /** @deprecated Use FragmentScenario instead */
    @Deprecated
    public static <T extends Fragment> FragmentController<T> buildFragment(Class<T> fragmentClass, Intent intent);
    
    /** @deprecated Use FragmentScenario instead */
    @Deprecated  
    public static <T extends Fragment> FragmentController<T> buildFragment(
        Class<T> fragmentClass, 
        Intent intent, 
        Bundle arguments);
    
    /** @deprecated Use FragmentScenario instead */
    @Deprecated
    public static <T extends Fragment> FragmentController<T> buildFragment(
        Class<T> fragmentClass, 
        Class<? extends Activity> activityClass, 
        Intent intent);
    
    /** @deprecated Use FragmentScenario instead */
    @Deprecated
    public static <T extends Fragment> FragmentController<T> buildFragment(
        Class<T> fragmentClass, 
        Class<? extends Activity> activityClass, 
        Bundle arguments);
    
    /** @deprecated Use FragmentScenario instead */
    @Deprecated
    public static <T extends Fragment> FragmentController<T> buildFragment(
        Class<T> fragmentClass, 
        Class<? extends Activity> activityClass, 
        Intent intent, 
        Bundle arguments);
}

BackupAgent Management

Factory methods and controller for BackupAgent lifecycle management.

public class Robolectric {
    /** Creates BackupAgentController for the given backup agent class */
    public static <T extends BackupAgent> BackupAgentController<T> buildBackupAgent(Class<T> backupAgentClass);
    
    /** Creates and sets up backup agent */
    public static <T extends BackupAgent> T setupBackupAgent(Class<T> backupAgentClass);
}

BackupAgentController<T>

Controller for managing BackupAgent lifecycle.

/**
 * Controller for managing BackupAgent lifecycle.
 */
public class BackupAgentController<T extends BackupAgent> 
    extends ComponentController<BackupAgentController<T>, T> {
    
    public BackupAgentController<T> create();
    public T get();
}

ComponentController<C, T>

Abstract base class for all component controllers providing common functionality.

/**
 * Base class for all component controllers.
 */
public abstract class ComponentController<C extends ComponentController<C, T>, T> {
    protected final C myself;
    protected T component;
    protected final ShadowLooper shadowMainLooper;
    protected Intent intent;
    protected boolean attached;
    
    public ComponentController(T component);
    public ComponentController(T component, Intent intent);
    
    public abstract T get();
}

Best Practices

  1. Use Modern Alternatives: Prefer ActivityScenario and FragmentScenario from AndroidX Test for higher-level, more maintainable tests.

  2. Proper Lifecycle Management: Always call lifecycle methods in the correct order that matches Android framework behavior.

  3. Resource Cleanup: Use try-with-resources or explicit cleanup to avoid memory leaks in tests.

  4. Thread Safety: Component creation and lifecycle methods must be called on the main thread.

  5. Configuration Changes: Use configurationChange() instead of recreating activities to test configuration changes properly.

Install with Tessl CLI

npx tessl i tessl/maven-org-robolectric--robolectric

docs

component-lifecycle.md

index.md

looper-threading.md

runtime-environment.md

shadow-system.md

test-runner.md

tile.json