Robolectric 3.1和AppCompatTheme错误

时间:2016-07-07 17:23:30

标签: android unit-testing robolectric

无法设置活动。我有错误:

java.lang.RuntimeException: Could not find any resource  from reference ResName{com.***.***: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:525)
at org.robolectric.shadows.ShadowAssetManager$StyleResolver.getAttrValue(ShadowAssetManager.java:463)
at org.robolectric.shadows.ShadowAssetManager.getOverlayedThemeValue(ShadowAssetManager.java:748)
at org.robolectric.shadows.ShadowAssetManager.findAttributeValue(ShadowAssetManager.java:737)
at org.robolectric.shadows.ShadowAssetManager.buildAttributes(ShadowAssetManager.java:683)
at org.robolectric.shadows.ShadowResources.attrsToTypedArray(ShadowResources.java:103)
at org.robolectric.shadows.ShadowResources.access$000(ShadowResources.java:52)
at org.robolectric.shadows.ShadowResources$ShadowTheme.obtainStyledAttributes(ShadowResources.java:270)
at org.robolectric.shadows.ShadowResources$ShadowTheme.obtainStyledAttributes(ShadowResources.java:265)
at org.robolectric.shadows.ShadowResources$ShadowTheme.obtainStyledAttributes(ShadowResources.java:260)
at android.content.res.Resources$Theme.obtainStyledAttributes(Resources.java)
at android.app.Activity.onApplyThemeResource(Activity.java:3727)
at android.view.ContextThemeWrapper.initializeTheme(ContextThemeWrapper.java:150)
at android.view.ContextThemeWrapper.setTheme(ContextThemeWrapper.java:94)
at android.support.v7.app.AppCompatActivity.setTheme(AppCompatActivity.java:90)
at org.robolectric.shadows.ShadowActivity.setThemeFromManifest(ShadowActivity.java:94)
at org.robolectric.shadows.CoreShadowsAdapter$1.setThemeFromManifest(CoreShadowsAdapter.java:30)
at org.robolectric.util.ActivityController.attach(ActivityController.java:70)
at org.robolectric.util.ActivityController.of(ActivityController.java:32)
at org.robolectric.Robolectric.buildActivity(Robolectric.java:82)
at org.robolectric.Robolectric.buildActivity(Robolectric.java:78)
at org.robolectric.Robolectric.setupActivity(Robolectric.java:86)
at com.***.***.ui.activities.LoginActivityTest.setUp(LoginActivityTest.java:23)
at  org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:24)
at org.robolectric.RobolectricTestRunner$2.evaluate(RobolectricTestRunner.java:265)
at org.robolectric.RobolectricTestRunner.runChild(RobolectricTestRunner.java:191)
at org.robolectric.RobolectricTestRunner.runChild(RobolectricTestRunner.java:56)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.robolectric.RobolectricTestRunner$1.evaluate(RobolectricTestRunner.java:157)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runners.Suite.runChild(Suite.java:128)
at org.junit.runners.Suite.runChild(Suite.java:27)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:69)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:234)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:74)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)

我发现此问题已在3.0版中修复,但我在3.1版中仍然存在此错误。我使用@RunWith(RobolectricGradleTestRunner.class)。我做错了什么?

我的Gradle:

compileSdkVersion 24
buildToolsVersion "24.0.0"
...
minSdkVersion 15
targetSdkVersion 23

sourceSets {
    main { java.srcDirs = ['src/main/java'] }
    test { java.srcDirs = ['src/test/java'] }
}

testCompile 'junit:junit:4.12'
testCompile 'org.assertj:assertj-core:1.7.1'

testCompile'org.robolectric:shadows-support-v4:3.1'
testCompile("org.robolectric:robolectric:3.1") {
    exclude module: 'classworlds'
    exclude module: 'commons-logging'
    exclude module: 'httpclient'
    exclude module: 'maven-artifact'
    exclude module: 'maven-artifact-manager'
    exclude module: 'maven-error-diagnostics'
    exclude module: 'maven-model'
    exclude module: 'maven-project'
    exclude module: 'maven-settings'
    exclude module: 'plexus-container-default'
    exclude module: 'plexus-interpolation'
    exclude module: 'plexus-utils'
    exclude module: 'wagon-file'
    exclude module: 'wagon-http-lightweight'
    exclude module: 'wagon-provider-api'
    exclude group: 'com.android.support', module: 'appcompat'
    exclude group: 'com.android.support', module: 'support-v4'
    exclude group: 'com.android.support', module: 'recyclerview-v7'
}

compile 'com.joshdholtz.sentry:sentry-android:1.4.1'
compile 'com.android.support:design:24.0.0'
compile 'com.android.support:recyclerview-v7:24.0.0'
compile 'com.android.support:support-v4:24.0.0'
compile 'com.android.support:appcompat-v7:24.0.0'
compile 'com.google.android.gms:play-services-auth:9.0.2'
compile 'com.google.android.gms:play-services-maps:9.0.2'
compile 'com.google.android.gms:play-services-location:9.0.2'
compile 'com.google.android.gms:play-services-analytics:9.0.2'
compile 'com.squareup.retrofit2:retrofit:2.0.2'
compile 'com.squareup.retrofit2:converter-jackson:2.0.2'
compile 'com.squareup.okhttp3:logging-interceptor:3.0.1'
compile 'com.squareup.picasso:picasso:2.5.2'
compile 'com.vk:androidsdk:1.3.9'
compile 'com.facebook.android:facebook-android-sdk:4.5.0'
compile 'com.alexvasilkov:gesture-views:2.1.1'
compile 'com.github.bumptech.glide:glide:3.6.1'
compile 'com.github.tajchert:nammu:1.1.1'
compile('com.sangcomz:FishBun:0.4.4@aar') {
    transitive = true
}

我的LoginActivityTest:

@RunWith(RobolectricGradleTestRunner.class)
@Config(constants = BuildConfig.class, sdk = 23)
public class LoginActivityTest {

    @Before
    public void setUp() {
        Robolectric.setupActivity(LoginActivity.class);
    }

    @Test
    public void testSholdNotBeNull() throws Exception {
        assertTrue(true);
    }
}

我的主题:

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">

    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
    <item name="android:windowBackground">@color/colorBackground</item>

    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>

    <item name="android:textColorPrimary">@color/textColorPrimary</item>
    <item name="android:textColorSecondary">@color/textColorSecondary</item>
    <item name="android:textColorPrimaryInverse">@color/textColorPrimaryDisableOnly</item>

</style>

0 个答案:

没有答案