自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
Run Code Online (Sandbox Code Playgroud)
...
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'
Run Code Online (Sandbox Code Playgroud)
...
androidTestCompile('org.robolectric:robolectric:2.4-SNAPSHOT') {
Run Code Online (Sandbox Code Playgroud)
我在这里提出了一个问题: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) …Run Code Online (Sandbox Code Playgroud) 我正在尝试测试我的 recyclerView 并且我正在使用材料卡视图进行项目显示,虽然该应用程序运行良好,但当我尝试测试时出现此错误:
android.view.InflateException: Binary XML file line #11: Binary XML file line #11: Error inflating class com.google.android.material.card.MaterialCardView
Caused by: android.view.InflateException: Binary XML file line #11: Error inflating class com.google.android.material.card.MaterialCardView
Caused by: java.lang.reflect.InvocationTargetException
...
Caused by: java.lang.IllegalArgumentException: The style on this component requires your app theme to be Theme.MaterialComponents (or a descendant).
Run Code Online (Sandbox Code Playgroud)
现在测试就这么简单:
@Test
fun shouldShowList() {
launchFragmentInContainer<PostsFragment>()
Thread.sleep(5000)
}
Run Code Online (Sandbox Code Playgroud)
睡眠仅用于应用程序等待尝试显示列表。奇怪的是,当我的列表项布局不使用 materialCardView 时,测试通过了。现在我已将我的应用主题更改为:
<style name="AppTheme" parent="Theme.MaterialComponents.Light.NoActionBar">
Run Code Online (Sandbox Code Playgroud)
但测试仍然消失,那么如何更改我的应用程序的测试主题?