与Robolectric运行Android棒棒糖Appcompat问题 [英] Android Lolipop Appcompat problems running with Robolectric
问题描述
我不能使用可用的新Appcompat支持库时,由于Android棒棒糖出来跑Robolectic测试。我跟着:
I'm not able to run Robolectic test when using new Appcompat support library available since Android Lolipop came out. I've followed:
- <一个href="https://github.com/robolectric/deckard-gradle">https://github.com/robolectric/deckard-gradle
- <一个href="https://chris.banes.me/2014/10/17/appcompat-v21/">https://chris.banes.me/2014/10/17/appcompat-v21/
- https://github.com/robolectric/deckard-gradle
- https://chris.banes.me/2014/10/17/appcompat-v21/
我目前的进展情况,请访问:<一href="https://github.com/fada21/android-tdd-bootstrap">https://github.com/fada21/android-tdd-bootstrap
My current progress is available here: 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') {
我在这里提出一个问题:<一href="https://github.com/robolectric/robolectric/issues/1332">https://github.com/robolectric/robolectric/issues/1332 (在这里寻找更多详情)。
I've raised an issue here: https://github.com/robolectric/robolectric/issues/1332 (look here for more details).
这是错误,我越来越:
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)
有什么想法?
推荐答案
直到Robolectric 3.0出来的时候,这里有一个修复。
Until Robolectric 3.0 comes out, here's a fix.
#/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类
And then adjust your custom RobolectricRunner class
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使用不同的应用程序的主题。然后用这个的TestRunner像通常那样用 @RunWith(MyRobolectricTestRunner.class)
。
注意:
这解决的活动,只有延长活动
,同类型的其他问题发生的活动扩展ActionBarActivity
Note:
This addresses activities that only extend Activity
, other issues of the same type occur for activities that extend ActionBarActivity
编辑:截至15年4月7日,Robolectric 3.0快照版本可占 ActionBarActivity
。在评论的链接的详细信息,请
As of 4/7/15, Robolectric 3.0-snapshot build is available which accounts for ActionBarActivity
. More information is available in the links in the comments
这篇关于与Robolectric运行Android棒棒糖Appcompat问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!