Automated Migration

Robolectric provides an automated migration tool to help keep your test suite up to date with Robolectric API changes. It’s based on Error Prone’s refactoring tools.

The migration tool will make changes directly to source files in your codebase, which you can review and commit to your source control system.

Before updating your dependencies to the new version of Robolectric:

  1. Make sure you’re using a recent version of Gradle (4.10 or newer).

  2. Configure your project to compile using Error Prone. Quick config for Gradle (usually in app/build.gradle):

     plugins {
         id "net.ltgt.errorprone" version "0.6" apply false
     }
    
     String roboMigration = System.getenv("ROBOLECTRIC_MIGRATION")
     if (roboMigration) {
         apply plugin: "net.ltgt.errorprone"
    
         dependencies {
             errorprone "com.google.errorprone:error_prone_core:2.3.2"
             errorproneJavac "com.google.errorprone:javac:9+181-r4173-1"
    
             errorprone "org.robolectric:errorprone:4.12-SNAPSHOT"
         }
    
         afterEvaluate {
             tasks.withType(JavaCompile) { t ->
                 options.errorprone.errorproneArgs += [
                         '-XepPatchChecks:' + roboMigration,
                         '-XepPatchLocation:IN_PLACE',
                 ]
             }
         }
     }
    

    You don’t need to commit this change.

  3. Run the migrations. Due to limitations of Error Prone, you’ll need to manually do this in a couple steps:

     ROBOLECTRIC_MIGRATION=DeprecatedMethods ./gradlew clean :compileDebugUnitTestJava
     ROBOLECTRIC_MIGRATION=ShadowUsageCheck ./gradlew clean :compileDebugUnitTestJava
    
  4. Make sure your code still compiles and commit changes.

  5. Update your project to the new version of Robolectric.

The migration tool will make a best effort attempt to adjust source, but there might be more complicated situations that it cannot handle and that need to be converted manually.

Robolectric
  • Home
  • Setup
    • Getting Started
    • Writing Your First Test
  • User Guide
    • AndroidX Test
    • Configuring Robolectric
    • Device Configuration
    • Using Qualified Resources
    • Using Add-On Modules
    • Best Practices
  • Customizing
    • Build System Integration
    • Customizing the Test Runner
  • Contributing
    • Building Robolectric
    • Contributor Guidelines
    • Shadows
  • Resources
    • GitHub
    • Release Notes
    • Migration Guide
    • Javadoc: 3.8 | 4.0 | 4.1 | 4.2 | 4.3 | 4.4 | 4.5 | 4.6 | 4.7 | 4.8 | 4.9 | 4.10 | 4.11
    • Issues

Recent Posts

Improving android-all Downloading on CI

Nov 11, 2023

In recent years, the team has received multiple issues regarding...

GSoC 2022 - ConscryptMode

Sep 6, 2022

My name is Umesh Singh and I was an...

sharedTest pattern: sharing tests and speeding up development

Oct 6, 2021

After Robolectric’s 4.0 release, Robolectric supports...

  • More…

Chrome Extension

See Robolectric documentation inline with Android Developer Documentation.

Contact

  • GitHub Project
  • robolectric@googlegroups.com
  • @Robolectric

©2010-2017. All rights reserved.
Design: HTML5 UP.