Skip to content

Instantly share code, notes, and snippets.

@sonique6784
Last active March 10, 2017 01:21
Show Gist options
  • Save sonique6784/5d87ae1042f59a2a13a44abfab5e42b3 to your computer and use it in GitHub Desktop.
Save sonique6784/5d87ae1042f59a2a13a44abfab5e42b3 to your computer and use it in GitHub Desktop.
Robolectric Flavour Test Runner (fix paths when you have an app with flavouring)
package au.com.myapp;
import org.junit.runners.model.InitializationError;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.annotation.Config;
import org.robolectric.manifest.AndroidManifest;
import org.robolectric.res.FileFsFile;
import org.robolectric.util.ReflectionHelpers;
/**
* Created by cedric on 9/03/2017.
* Robolectric: 3.3.1
*
* Usage:
* Add the following line at above your Test class name
@RunWith(RobolectricFlavourTestRunner.class)
@Config(constants = BuildConfig.class, sdk = 24)
public class MainActivityTest {
@Test
public void mainActivityCanStart() {
MainActivity mainActivity = Robolectric.setupActivity(MainActivity.class);
assertNotNull(mainActivity);
}
}
*/
public class RobolectricFlavourTestRunner extends RobolectricTestRunner {
private static final String BUILD_OUTPUT = "app/build/intermediates"; // this fix the path
public RobolectricFlavourTestRunner(Class<?> klass) throws InitializationError {
super(klass);
}
@Override
protected AndroidManifest getAppManifest(Config config) {
if (config.constants() == Void.class) {
//Log.e("RobolectricFlavorTest", "Field 'constants' not specified in @Config annotation");
//Log.e("RobolectricFlavorTest", "This is required when using RobolectricGradleTestRunner!");
throw new RuntimeException("No 'constants' field in @Config annotation!");
}
final String type = getType(config);
final String flavor = getFlavor(config);
final String applicationId = getApplicationId(config);
final FileFsFile res;
if (FileFsFile.from(BUILD_OUTPUT, "res", flavor, type).exists()) {
res = FileFsFile.from(BUILD_OUTPUT, "res", flavor, type);
} else {
// Use res/merged if the output directory doesn't exist for Data Binding compatibility
res = FileFsFile.from(BUILD_OUTPUT, "res/merged", flavor, type);
}
final FileFsFile assets = FileFsFile.from(BUILD_OUTPUT, "assets", flavor, type);
final FileFsFile manifest;
if (FileFsFile.from(BUILD_OUTPUT, "manifests").exists()) {
manifest = FileFsFile.from(BUILD_OUTPUT, "manifests", "full", flavor, type, "AndroidManifest.xml");
} else {
// Fallback to the location for library manifests
manifest = FileFsFile.from(BUILD_OUTPUT, "bundle", flavor, type, "AndroidManifest.xml");
}
System.out.println("Robolectric assets directory: " + assets.toString());
System.out.println(" Robolectric res directory: " + res.toString());
System.out.println(" Robolectric manifest path: " + manifest.toString());
System.out.println(" Robolectric package name: " + applicationId);
return new AndroidManifest(manifest, res, assets, applicationId);
}
private String getType(Config config) {
try {
return ReflectionHelpers.getStaticField(config.constants(), "BUILD_TYPE");
} catch (Throwable e) {
return null;
}
}
private String getFlavor(Config config) {
try {
return ReflectionHelpers.getStaticField(config.constants(), "FLAVOR");
} catch (Throwable e) {
return null;
}
}
private String getApplicationId(Config config) {
try {
return ReflectionHelpers.getStaticField(config.constants(), "APPLICATION_ID");
} catch (Throwable e) {
return null;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment