Created
August 27, 2014 21:44
-
-
Save flo-genymobile/0a40acab0bfc56d10b07 to your computer and use it in GitHub Desktop.
Robolectric Tips And Tricks to add to android project
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
##### dependencies to add for robolectric + fest-android | |
--> in top-level build.gradle | |
dependencies { | |
... | |
classpath 'org.robolectric:robolectric-gradle-plugin:0.12.+' | |
} | |
--> in build.gradle | |
{ | |
... | |
apply plugin: 'robolectric' | |
... | |
dependencies { | |
... | |
androidTestCompile 'org.hamcrest:hamcrest-integration:1.1' | |
androidTestCompile 'org.hamcrest:hamcrest-core:1.1' | |
androidTestCompile 'org.hamcrest:hamcrest-library:1.1' | |
androidTestCompile 'com.squareup:fest-android:1.0.+' | |
androidTestCompile('junit:junit:4.+') { | |
exclude module: 'hamcrest-core' | |
} | |
androidTestCompile('org.robolectric:robolectric:2.3') { | |
exclude module: 'classworlds' | |
exclude module: 'commons-logging' | |
exclude module: 'httpclient' | |
exclude module: 'maven-artifact' | |
exclude module: 'maven-artifact-manager' | |
exclude module: 'maven-error-diagnostics' | |
exclude module: 'maven-model' | |
exclude module: 'maven-project' | |
exclude module: 'maven-settings' | |
exclude module: 'plexus-container-default' | |
exclude module: 'plexus-interpolation' | |
exclude module: 'plexus-utils' | |
exclude module: 'support-v4' | |
exclude module: 'wagon-file' | |
exclude module: 'wagon-http-lightweight' | |
exclude module: 'wagon-provider-api' | |
} | |
... | |
} | |
} | |
##### Add test folder sitting next to the main folder of Android application, under the src/ folder | |
src/ | |
|------androidTest/java/com.bla.myapp.test | |
|------main/java/com.bla.myapp | |
|------main/res/stuff/bla.jpeg | |
|------AndroidManifest.xml | |
##### Need custom runner to generate AndroidManifest on the fly with targat API 18 as Robolectric doesn't support above | |
public class GenyRobolectricTestRunner extends RobolectricTestRunner { | |
private static final int MAX_SDK_SUPPORTED_BY_ROBOLECTRIC = 18; | |
public GenyRobolectricTestRunner(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)){ | |
@Override | |
public int getTargetSdkVersion() { | |
return MAX_SDK_SUPPORTED_BY_ROBOLECTRIC; | |
} | |
}; | |
} | |
AndroidManifest appManifest = super.getAppManifest(config); | |
return appManifest; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment