Skip to content

Instantly share code, notes, and snippets.

@lordcodes
Last active February 18, 2016 21:14
Show Gist options
  • Save lordcodes/fd5bfd5460e57747fd87 to your computer and use it in GitHub Desktop.
Save lordcodes/fd5bfd5460e57747fd87 to your computer and use it in GitHub Desktop.
Robolectric Gradle test runner so that Config annotation isn't required on each of your test classes.
public class CustomRobolectricTestRunner extends RobolectricGradleTestRunner {
private static final int MAX_SDK_LEVEL = 21;
public CustomRobolectricTestRunner(Class<?> testClass) throws InitializationError {
super(testClass);
}
@Override
public Config getConfig(Method method) {
Config config = super.getConfig(method);
config = new Config.Implementation(
ensureSdkLevels(config.sdk()),
config.manifest(),
config.qualifiers(),
config.packageName(),
config.resourceDir(),
config.assetDir(),
config.shadows(),
config.application(),
config.libraries(),
ensureBuildConfig(config.constants()));
return config;
}
private Class<?> ensureBuildConfig(Class<?> constants) {
if (constants == Void.class) return BuildConfig.class;
return constants;
}
private int[] ensureSdkLevels(int[] sdkLevels) {
int numberSdks = sdkLevels.length;
int[] sdks = new int[numberSdks];
for (int i = 0; i < numberSdks; i++) {
int sdk = sdks[i];
if (sdk > MAX_SDK_LEVEL || sdk <= 0) {
sdks[i] = MAX_SDK_LEVEL;
} else {
sdks[i] = sdk;
}
}
return sdks;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment