Skip to content

Instantly share code, notes, and snippets.

@dant3
Created November 14, 2013 08:27
Embed
What would you like to do?
AndroidTestRunner that works around the problem with different package name in manifest and R class for Robolectric
package com.myapp.whatever;
import org.junit.runners.model.InitializationError;
import org.robolectric.AndroidManifest;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.res.FsFile;
import com.myapp.R;
public class AndroidTestRunner extends RobolectricTestRunner {
public AndroidTestRunner(Class<?> testClass) throws InitializationError {
super(testClass);
}
@Override
protected AndroidManifest createAppManifest(FsFile manifestFile, FsFile resDir, FsFile assetsDir) {
return new CustomPackageManifest(manifestFile, resDir, assetsDir);
}
private static class CustomPackageManifest extends AndroidManifest {
public CustomPackageManifest(FsFile androidManifestFile, FsFile resDirectory, FsFile assetsDirectory) {
super(androidManifestFile, resDirectory, assetsDirectory);
}
@Override
public String getRClassName() throws Exception {
return getRClass().getName();
}
@Override
public Class<?> getRClass() {
return R.class;
}
@Override
public String getPackageName() {
return R.class.getPackage().getName();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment