自 Android Lollipop 发布以来,在使用可用的新 Appcompat 支持库时,我无法运行 Robolectic 测试。我关注了:
我目前的进度可以在这里找到:https://github.com/fada21/android-tdd-bootstrap
我的配置(蒸馏)是:
android {
compileSdkVersion 21
buildToolsVersion "21.0.1"
defaultConfig {
applicationId "com.fada21.android.bootstrap"
minSdkVersion 15
targetSdkVersion 21
...
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:support-v4:21.0.0'
compile 'com.android.support:appcompat-v7:21.0.0'
...
androidTestCompile('org.robolectric:robolectric:2.4-SNAPSHOT') {
我在这里提出了一个问题:https://github.com/robolectric/robolectric/issues/1332(查看此处了解更多详细信息)。
这是我遇到的错误:
java.lang.RuntimeException: Could not find any resource from reference ResName{com.fada21.android.bootstrap:style/Theme_AppCompat_Light_NoActionBar} from style StyleData{name='AppTheme', parent='Theme_AppCompat_Light_NoActionBar'} 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:297)
at org.robolectric.shadows.ShadowResources.findAttributeValue(ShadowResources.java:286)
at org.robolectric.shadows.ShadowResources.attrsToTypedArray(ShadowResources.java:189)
at org.robolectric.shadows.ShadowResources.access$000(ShadowResources.java:48)
at org.robolectric.shadows.ShadowResources$ShadowTheme.obtainStyledAttributes(ShadowResources.java:494)
at org.robolectric.shadows.ShadowResources$ShadowTheme.obtainStyledAttributes(ShadowResources.java:489)
at org.robolectric.shadows.ShadowResources$ShadowTheme.obtainStyledAttributes(ShadowResources.java:484)
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:143)
at android.support.v7.app.ActionBarActivityDelegateBase.onCreate(ActionBarActivityDelegateBase.java:139)
at android.support.v7.app.ActionBarActivity.onCreate(ActionBarActivity.java:123)
at com.fada21.android.bootstrap.HomeActivity.onCreate(HomeActivity.java:28)
at android.app.Activity.performCreate(Activity.java:5133)
at org.fest.reflect.method.Invoker.invoke(Invoker.java:112)
at org.robolectric.util.ActivityController$1.run(ActivityController.java:113)
at org.robolectric.shadows.ShadowLooper.runPaused(ShadowLooper.java:265)
at org.robolectric.util.ActivityController.create(ActivityController.java:110)
at org.robolectric.util.ActivityController.create(ActivityController.java:120)
at com.fada21.android.bootstrap.HomeActivityTest.testActivityNotNull(HomeActivityTest.java:24)
最佳答案
注意:截至 2015 年 7 月 7 日,Roboelectric 3.0已被释放。它解决了问题,使这个答案不再是必要的。
旧答案:
在 Robolectric 3.0 发布之前,这里有一个修复方法。
#/app/src/main/res/values/styles.xml
<resources>
//<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
//<!-- Customize your theme here. -->
</style>
//<!-- Hack for Robolectric to run with appcompat.v7 -->
<style name="RoboAppTheme" parent="@android:style/Theme.Holo.Light.DarkActionBar">
//<!-- Customize your theme here. -->
</style>
</resources>
然后调整您的自定义 RobolectricRunner 类
public class MyRobolectricTestRunner extends RobolectricTestRunner {
private static final int MAX_SDK_SUPPORTED_BY_ROBOLECTRIC = 18;
public MyRobolectricTestRunner(Class<?> testClass) throws InitializationError {
super(testClass);
}
@Override
protected AndroidManifest getAppManifest(Config config) {
String manifestProperty = "../app/src/main/AndroidManifest.xml";
String resProperty = "../app/src/main/res";
return new AndroidManifest(Fs.fileFromPath(manifestProperty), Fs.fileFromPath(resProperty)) {
@Override
public int getTargetSdkVersion() {
return MAX_SDK_SUPPORTED_BY_ROBOLECTRIC;
}
@Override
public String getThemeRef(Class<? extends Activity> activityClass) {
return "@style/RoboAppTheme";
}
};
}
}
基本上,我们只是告诉 JVM 使用不同的应用主题。然后像通常使用 @RunWith(MyRobolectricTestRunner.class)
一样使用此 TestRunner。
注意:
这解决了仅 extend Activity
的 Activity ,extend ActionBarActivity
编辑:自 2015 年 4 月 7 日起,Robolectric 3.0 快照构建可用,其中包含 ActionBarActivity
。评论中的链接提供了更多信息
关于使用 Robolectric 运行的 Android Lollipop Appcompat 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26512839/