Non riesco a configurare il mio progetto Android Studio con Robolectric. Qualcuno potrebbe guidarmi passo dopo passo a costruire un progetto Android Studio 1.1.0 con Robolectric 2.4 & gradle 2.2.1Come utilizzare Robolectric 2.4 in Android Studio 1.1.0 e gradle 2.2.1
Questo esempio di progetti funziona bene https://github.com/robolectric/deckard-gradle. Ma non sono in grado di implementarlo nei miei progetti Android Studio esistenti.
// seguito sono la descrizione dettagliata
// contenuto del progetto di build.gradle
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:1.1.0'
}
}
allprojects {
repositories {
jcenter()
}
}
// contenuto del 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"
}
// contenuto del 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));
}
}
// ERRORE
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)
Quale problema esatto hai e? Qualche errore registra? –