web-dev-qa-db-ja.com

Robolectric:IllegalStateException:このアクティビティではTheme.AppCompatテーマ(または子孫)を使用する必要があります

私はAndroidでの一般的なロボレクトリックおよびインストルメンテーションテストに不慣れです。私は最初のテストをテストするために Robolectricのガイド に沿って進みました。しかし、私は得ています:

Java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.

エラーを検索しましたが、解決策がありません。このエラーを引き起こす可能性のあるすべてのものを含めています。

Gradle:

apply plugin: 'com.Android.application'
apply plugin: 'com.neenbedankt.Android-apt' //needed for source code generation
apply plugin: 'jacoco'
apply plugin: 'de.mobilej.unmock'

Android {
    compileSdkVersion 22
    buildToolsVersion "22.0.1"

    testOptions {
        unitTests.returnDefaultValues = true
    }

    defaultConfig {
        applicationId "#######"
        minSdkVersion 15
        targetSdkVersion 22
        testHandleProfiling true
        testFunctionalTest true
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        debug{
            testCoverageEnabled false
        }
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-Android.txt'), 'proguard-rules.pro'
        }
    }

    lintOptions {
        abortOnError false
    }

    jacoco {
        version "0.7.1.201405082137"
    }

    packagingOptions {
        exclude 'META-INF/DEPENDENCIES.txt'
        exclude 'META-INF/LICENSE.txt'
        exclude 'META-INF/NOTICE.txt'
        exclude 'META-INF/NOTICE'
        exclude 'META-INF/LICENSE'
        exclude 'META-INF/DEPENDENCIES'
        exclude 'META-INF/notice.txt'
        exclude 'META-INF/license.txt'
        exclude 'META-INF/dependencies.txt'
        exclude 'META-INF/LGPL2.1'
        exclude 'META-INF/services/javax.annotation.processing.Processor'
        exclude 'LICENSE.txt'
    }

    jacoco {
        version "0.7.1.201405082137"
    }
}

def coverageSourceDirs = [
        '../app/src/main/Java'
]

unMock {
    // URI to download the Android-all.jar from. e.g. https://oss.sonatype.org/content/groups/public/org/robolectric/Android-all/
    allAndroid =
            'https://oss.sonatype.org/content/groups/public/org/robolectric/Android-all/4.3_r2-robolectric-0/Android-all-4.3_r2-robolectric-0.jar'

    // classes to keep
    // matched by "startsWith" - you also need to include the dependencies manually
    // if you start with an "-" the class will match by "equals" (and it will additionally  match
    // inner classes of this class)
    keep = [
            "Android.view.LayoutInflater",
            "Android.support.v7.app.AppCompatActivity"
    ]
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.Android.support:appcompat-v7:22.2.1'
    compile 'com.Android.support:recyclerview-v7:22.0.+'
    compile 'com.Android.support:cardview-v7:22.0.+'

    //Easy parsing JSON to POJO and vice versa
    compile 'com.google.code.gson:gson:2.3.1'

    //Photo's from URL to ImageView
    compile 'com.squareup.picasso:picasso:2.5.2'

    //REST API into an interface
    compile 'com.squareup.retrofit:retrofit:1.9.0'

    //Dependency Injection
    compile 'com.google.dagger:dagger:2.0.1'
    apt 'com.google.dagger:dagger-compiler:2.0.1'
    provided 'org.glassfish:javax.annotation:10.0-b28'
    compile 'com.jakewharton:butterknife:7.0.1'

    //Realm
    compile 'io.realm:realm-Android:0.82.1'

    //RxAndroid (old version)
    compile 'io.reactivex:rxandroid:0.24.0'

    testCompile 'org.mockito:mockito-core:1.10.19'

    //Powermock
    testCompile ('org.powermock:powermock-api-mockito:1.6.2'){
        exclude group: 'org.mockito'
    }
    testCompile 'org.powermock:powermock-module-junit4:1.6.2'

    //Testing Framework
    //compile 'org.mockito:mockito-core:2.0.31-beta'
    testCompile 'junit:junit:4.12'

    //Robolectric
    testCompile "org.robolectric:robolectric:2.4"
}

マニフェスト:

<uses-sdk
        Android:minSdkVersion="15"
        Android:targetSdkVersion="22" />
<application
        Android:allowBackup="true"
        Android:icon="@mipmap/ic_launcher"
        Android:label="@string/app_name"
        Android:theme="@style/AppTheme" >
        <activity
            Android:name=".ui.MainActivity"
            Android:label="@string/title">
            <intent-filter>
                <action Android:name="Android.intent.action.MAIN" />
                <category Android:name="Android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
</application>

values/styles.xml

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <!-- colorPrimary is used for the default action bar background -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <!-- colorPrimaryDark is used for the status bar -->
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <!-- colorAccent is used as the default value for colorControlActivated,
     which is used to tint widgets -->
    <item name="colorAccent">@color/accent</item>

    <!-- set the application's background color -->
    <item name="Android:colorBackground">@color/icons</item>
</style>

MainActivity.Java

public class MainActivity extends AppCompatActivity{
    //implementation
}

MainActivityTest.Java

@RunWith(RobolectricTestRunner.class)
public class MainActivityRoboTest {

    @Test
    public void test_onCreate_RecyclerViewShouldBeAvailable() throws Exception {
        MainActivity activity = Robolectric.setupActivity(MainActivity.class);
        RecyclerView r = (RecyclerView) activity.findViewById(R.id.rv);
    }
}

編集

完全なスタックトレース

Java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
    at Android.support.v7.app.AppCompatDelegateImplBase.onCreate(AppCompatDelegateImplBase.Java:124)
    at Android.support.v7.app.AppCompatDelegateImplV7.onCreate(AppCompatDelegateImplV7.Java:146)
    at Android.support.v7.app.AppCompatActivity.onCreate(AppCompatActivity.Java:59)
    at ui.MainActivity.onCreate(MainActivity.Java:38)
    at Android.app.Activity.performCreate(Activity.Java:5008)
    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 org.robolectric.util.ActivityController.setup(ActivityController.Java:202)
    at org.robolectric.Robolectric.setupActivity(Robolectric.Java:1388)
    at ui.MainActivityRoboTest.test_onCreate_RecyclerViewShouldBeAvailable(MainActivityRoboTest.Java:21)
    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.InvokeMethod.evaluate(InvokeMethod.Java:17)
    at org.robolectric.RobolectricTestRunner$2.evaluate(RobolectricTestRunner.Java:236)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.Java:325)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.Java:78)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.Java:57)
    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:158)
    at org.junit.runners.ParentRunner.run(ParentRunner.Java:363)
    at org.junit.runner.JUnitCore.run(JUnitCore.Java:137)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.Java:140)
18
tim

同じ問題を修正しました。回避策は次のとおりです。

ダミーのアプリケーションを作成し、onCreate()内にテーマを設定します。

public class TestApplication extends Application {

    @Override
    public void onCreate() {
        super.onCreate();
        setTheme(R.style.AppTheme); //or just R.style.Theme_AppCompat
    }
}

次に、テスト内でconfigでアプリケーションクラスを指定します。

@RunWith(RobolectricTestRunner.class)
@Config(application = TestApplication.class)
public class YourActivityTest {
    //tests
}
22
Szymon Chaber

これを試してください:Androidマニフェストファイルを次のように変更します:

<application
    Android:allowBackup="true"
    Android:icon="@mipmap/ic_launcher"
    Android:label="@string/app_name"
    Android:theme="@style/Theme.AppCompat" >
    <activity
        Android:name=".ui.MainActivity"
        Android:label="@string/title">
        <intent-filter>
            <action Android:name="Android.intent.action.MAIN" />
            <category Android:name="Android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>
2
KISHORE_ZE

これはRobotelectricのバグです。回避策は次のとおりです。

Robolectricで実行されているAndroid Lolipop Appcompatの問題

Robotelectric 3.0にアップグレードして、これが修正されているかどうかを確認することもできます。

1
Derek Fung

代わりにRobolectricGradleTestRunnerを使用する必要があります。

import org.robolectric.RobolectricGradleTestRunner;

@RunWith(RobolectricGradleTestRunner.class)
@Config(constants = BuildConfig.class, manifest = Config.NONE)
public class MainActivityRoboTest {

それは私の問題を解決します。

0
jiahao

@RunWith(RobolectricTestRunner.class)を使用して幸せになりましょう

@RunWith(RobolectricTestRunner.class)
public class MainActivityTest {

    @Test
    public void myTest() throws Exception {
        assertEquals(true, true);
    }
}

詳細はこちらをご覧ください: http://robolectric.org/getting-started/

0
whalemare