I am unable to configure my Android Studio Project with Robolectric. Could any one guide me step by step to build an android studio 1.1.0 project with Robolectric 2.4 & gradle 2.2.1
This sample projects work fine https://github.com/robolectric/deckard-gradle. But I am not able to implement it in my existing Android Studio projects.
//below are the detailed description
//Contents Of of project build.gradle
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:1.1.0'
}
}
allprojects {
repositories {
jcenter()
}
}
//Contents Of app/src/build.gradle
apply plugin: 'com.android.application'
android {
compileSdkVersion 21
buildToolsVersion "21.1.2"
defaultConfig {
applicationId "robo.testing.android.testrobo08"
minSdkVersion 16
targetSdkVersion 21
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
sourceSets { main { java.srcDirs = ['src/main/java'] } }
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:21.0.3'
// Test Compile
testCompile 'junit:junit:4.12'
testCompile 'org.robolectric:robolectric:2.4'
testCompile "org.mockito:mockito-core:1.9.5"
}
//Contents Of MainActivityTest
package robo.testing.android.testrobo08.test;
import android.app.Activity;
import android.widget.TextView;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.Robolectric;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.annotation.Config;
import robo.testing.android.testrobo08.MainActivity;
import robo.testing.android.testrobo08.R;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.junit.Assert.assertThat;
/**
* Created by cflap584 on 13/3/15.
*/
@RunWith(RobolectricTestRunner.class)
@Config(manifest = "./src/main/AndroidManifest.xml", emulateSdk = 18)
public class MainActivityTest {
Activity activity = Robolectric.buildActivity(MainActivity.class).create().get();
TextView textView = (TextView) activity.findViewById(R.id.myTextView);
@Test
public void testTextView_lableName() throws Exception {
String actual = textView.getText().toString();
assertThat("Hello World!", equalTo(actual));
}
}
//ERROR
robo.testing.android.testrobo08.test.MainActivityTest > testTextView_lableName STANDARD_ERROR
WARNING: no system properties value for ro.build.date.utc
robo.testing.android.testrobo08.test.MainActivityTest > testTextView_lableName STANDARD_OUT
DEBUG: Loading resources for robo.testing.android.testrobo08 from ././src/main/res...
DEBUG: Loading resources for android from jar:/home/cflap584/.m2/repository/org/robolectric/android-all/4.3_r2-robolectric-0/android-all-4.3_r2-robolectric-0.jar!/res...
Gradle Test Executor 1 finished executing tests.
robo.testing.android.testrobo08.test.MainActivityTest > testTextView_lableName FAILED
java.lang.RuntimeException: Could not find any resource from reference ResName{robo.testing.android.testrobo08:style/Theme_AppCompat_Light_DarkActionBar} from style StyleData{name='AppTheme', parent='Theme_AppCompat_Light_DarkActionBar'} with theme null
at org.robolectric.shadows.ShadowAssetManager$StyleResolver.getParent(ShadowAssetManager.java:456)
at org.robolectric.shadows.ShadowAssetManager$StyleResolver.getAttrValue(ShadowAssetManager.java:394)
at org.robolectric.shadows.ShadowResources.getOverlayedThemeValue(ShadowResources.java:294)
at org.robolectric.shadows.ShadowResources.findAttributeValue(ShadowResources.java:283)
at org.robolectric.shadows.ShadowResources.attrsToTypedArray(ShadowResources.java:186)
at org.robolectric.shadows.ShadowResources.access$000(ShadowResources.java:44)
at org.robolectric.shadows.ShadowResources$ShadowTheme.obtainStyledAttributes(ShadowResources.java:491)
at org.robolectric.shadows.ShadowResources$ShadowTheme.obtainStyledAttributes(ShadowResources.java:486)
at org.robolectric.shadows.ShadowResources$ShadowTheme.obtainStyledAttributes(ShadowResources.java:481)
at android.content.res.Resources$Theme.obtainStyledAttributes(Resources.java)
at android.content.Context.obtainStyledAttributes(Context.java:380)
at android.support.v7.app.ActionBarActivityDelegate.onCreate(ActionBarActivityDelegate.java:147)
at android.support.v7.app.ActionBarActivityDelegateBase.onCreate(ActionBarActivityDelegateBase.java:138)
at android.support.v7.app.ActionBarActivity.onCreate(ActionBarActivity.java:123)
at robo.testing.android.testrobo08.MainActivity.onCreate(MainActivity.java:13)
at android.app.Activity.performCreate(Activity.java:5133)
at org.robolectric.internal.ReflectionHelpers$3.run(ReflectionHelpers.java:64)
at org.robolectric.internal.ReflectionHelpers.traverseClassHierarchy(ReflectionHelpers.java:114)
at org.robolectric.internal.ReflectionHelpers.callInstanceMethodReflectively(ReflectionHelpers.java:59)
at org.robolectric.util.ActivityController$1.run(ActivityController.java:115)
at org.robolectric.shadows.ShadowLooper.runPaused(ShadowLooper.java:268)
at org.robolectric.util.ActivityController.create(ActivityController.java:111)
at org.robolectric.util.ActivityController.create(ActivityController.java:122)
at robo.testing.android.testrobo08.test.MainActivityTest.<init>(MainActivityTest.java:26)
Robolectric is intended to be fully compatible with Android's official testing libraries since version 4.0. As such we encourage you to try these new APIs and provide feedback. At some point the Robolectric equivalents will be deprecated and removed.
Gradle Version 2.2.1
Android Plugin Version 1.1.0
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:1.1.0'
}
}
allprojects {
repositories {
jcenter()
}
}
apply plugin: 'com.android.application'
android {
compileSdkVersion 21
buildToolsVersion "21.1.2"
defaultConfig {
applicationId "robo.testing.android.testrobo"
minSdkVersion 16
targetSdkVersion 18
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
sourceSets { main { java.srcDirs = ['src/main/java'] } }
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:21.0.3'
// Test Compile
testCompile 'junit:junit:4.12'
testCompile 'org.robolectric:robolectric:2.4'
testCompile "org.mockito:mockito-core:1.9.5"
}
Test Artificat : Unit Tests
I found some reference for my solution
https://github.com/robolectric/robolectric/issues/1592
@erd If you are using Gradle / Android Studio, you need to create a custom test runner that will tell Robolectric where to find your merged assets, resources, and manifest. Please see the discussion here:
http://nenick-android.blogspot.in/2015/03/android-studio-110-and-robolectric-30.html
public CustomRobolectricRunner(Class<?> testClass)
throws InitializationError {
super(testClass);
String buildVariant = (BuildConfig.FLAVOR.isEmpty()
? "" : BuildConfig.FLAVOR+ "/") + BuildConfig.BUILD_TYPE;
String intermediatesPath = BuildConfig.class.getResource("")
.toString().replace("file:", "");
intermediatesPath = intermediatesPath
.substring(0, intermediatesPath.indexOf("/classes"));
System.setProperty("android.package",
BuildConfig.APPLICATION_ID);
System.setProperty("android.manifest",
intermediatesPath + "/manifests/full/"
+ buildVariant + "/AndroidManifest.xml");
System.setProperty("android.resources",
intermediatesPath + "/res/" + buildVariant);
System.setProperty("android.assets",
intermediatesPath + "/assets/" + buildVariant);
}
package robo.testing.android.testrobo.test;
import android.app.Activity;
import android.widget.TextView;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.Robolectric;
import robo.testing.android.testrobo.MainActivity;
import robo.testing.android.testrobo.R;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.core.IsEqual.equalTo;
/**
* Created by cflap584 on 16/3/15.
*/
@RunWith(CustomRobolectricTestRunner.class)
public class MainActivityTest {
Activity activity = Robolectric.buildActivity(MainActivity.class).create().get();
TextView textView = (TextView) activity.findViewById(R.id.myTextView);
@Test
public void testTextView_labelName() throws Exception {
String actual = textView.getText().toString();
assertThat("Hello world!", equalTo(actual));
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With