Skip to content

Support for Robolectric 2.2

Erich Douglass edited this page Jul 31, 2014 · 1 revision

Robolectric 2.2 or Earlier

Version 2.3 of Robolectric supports the plugin out of the box. If you are not able to use a recent version, you can use the custom runner below:

import org.junit.runners.model.InitializationError;
import org.robolectric.AndroidManifest;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.annotation.Config;
import org.robolectric.res.Fs;

public class RobolectricGradleTestRunner extends RobolectricTestRunner {
    public RobolectricGradleTestRunner(Class<?> testClass) throws InitializationError {
        super(testClass);
    }

    @Override protected AndroidManifest getAppManifest(Config config) {
        String manifestProperty = System.getProperty("android.manifest");
        if (config.manifest().equals(Config.DEFAULT) && manifestProperty != null) {
            String resProperty = System.getProperty("android.resources");
            String assetsProperty = System.getProperty("android.assets");
            return new AndroidManifest(Fs.fileFromPath(manifestProperty), Fs.fileFromPath(resProperty),
                Fs.fileFromPath(assetsProperty));
        }
        return super.getAppManifest(config);
    }
}

Annotate your test classes with @RunWith(RobolectricGradleTestRunner.class) or subclass this test runner if you have other customizations.

Clone this wiki locally