我无法使用Robolectric配置我的
Android Studio项目.任何人都可以一步一步地指导我使用Robolectric 2.4& amp;来构建一个android studio 1.1.0项目. gradle 2.2.1
此示例项目工作正常https://github.com/robolectric/deckard-gradle.但我无法在现有的Android Studio项目中实现它.
//下面是详细说明
//项目build.gradle的内容
buildscript { repositories { jcenter() } dependencies { classpath 'com.android.tools.build:gradle:1.1.0' } } allprojects { repositories { jcenter() } }
// 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" }
// 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)); } }
//错误
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)
解决方法
我发现了问题.在我的情况下,它无法找到AndroidManifest.xml文件&我没有使用customRobolectricTestRunner.
项目结构
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'),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
我找到了一些解决方案的参考资料
https://github.com/robolectric/robolectric/issues/1592
@erd如果您使用的是Gradle / Android Studio,则需要创建一个自定义测试运行器,告诉Robolectric在哪里可以找到合并的资源,资源和清单.请看这里的讨论:
http://nenick-android.blogspot.in/2015/03/android-studio-110-and-robolectric-30.html
src / test / java / packageName.test / CustomRobolectricRunner.java的内容
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); }
MainActivityTest.java的内容
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)); } }