Android Robolectric Configuration

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Extensions
> Step 2: And Like the video. BONUS: You can also share it!

Example

To configure robolectric add @Config annotation to test class or method.

Run with custom Application class

@RunWith(RobolectricTestRunner.class)
@Config(application = MyApplication.class)
public final class MyTest {
}

Set target SDK

@RunWith(RobolectricTestRunner.class)
@Config(sdk = Build.VERSION_CODES.LOLLIPOP)
public final class MyTest {
}

Run with custom manifest

When specified, robolectric will look relative to the current directory. Default value is AndroidManifest.xml

Resources and assets will be loaded relative to the manifest.

@RunWith(RobolectricTestRunner.class)
@Config(manifest = "path/AndroidManifest.xml")
public final class MyTest {
}

Use qualifiers

Possible qualifiers can be found in android docs.

@RunWith(RobolectricTestRunner.class)
public final class MyTest {

    @Config(qualifiers = "sw600dp")
    public void testForTablet() {
    }
}


Got any Android Question?