Please excuse my errant post. Seems I missed the critical fix where
Robolectric isn't playing nice with the gradle android way of running tests
and requires a special runner to find resources properly. For anyone else
who runs across this issue, you can solve it by using this custom runner:
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.Logger;
import org.robolectric.util.ReflectionHelpers;
/**
* Copied from Robolectric v3.0rc2 RobolectricGradleTestRunner
*/
public class MyRobolectricGradleTestRunner extends RobolectricTestRunner {
private static final String BUILD_OUTPUT = "build/intermediates";
public TRRobolectricGradleTestRunner (Class<?> klass) throws
InitializationError {
super(klass);
}
@Override
protected AndroidManifest getAppManifest(Config config) {
if (config.constants() == Void.class) {
Logger.error("Field 'constants' not specified in @Config
annotation");
Logger.error("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 = FileFsFile.from(BUILD_OUTPUT, "res", 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, "bundles", flavor, type,
"AndroidManifest.xml");
}
Logger.debug("Robolectric assets directory: " + assets.getPath());
Logger.debug(" Robolectric res directory: " + res.getPath());
Logger.debug(" Robolectric manifest path: " + manifest.getPath());
Logger.debug(" 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 config.constants().getPackage().getName();
} catch (Throwable e) {
return null;
}
}
// ------------------ inner classes ------------------
}
And then annotating all your test classes with:
@RunWith(MyRobolectricGradleTestRunner.class)
@Config(constants = BuildConfig.class, emulateSdk = 21)
--
You received this message because you are subscribed to the Google Groups
"adt-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/d/optout.