Android Robolectric - InflateException错误:(

时间:2014-10-13 04:26:42

标签: android testing unit-testing

我刚开始将roboelectric应用到我现有的项目中。 我有一个非常简单的登录页面的活动。两个EditTexts和一个Button。 当我进行测试时,这就是我得到的......

android.view.InflateException: XML file /Users/.../app/src/main/res/layout/a_login.xml line #-1 (sorry, not yet implemented): Error inflating class android.widget.EditText
at android.view.LayoutInflater.createView(LayoutInflater.java:620)
    at org.robolectric.shadows.RoboLayoutInflater.onCreateView(RoboLayoutInflater.java:38)
    at android.view.LayoutInflater.onCreateView(LayoutInflater.java:669)
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:694)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:758)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:758)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
    at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:267)
    at android.app.Activity.setContentView(Activity.java:1895)
    at com.example.pages.LoginActivity.onCreate(LoginActivity.java:61)

从最后一行可以看出,加载XML文件时会出现问题。 当我运行没有UI的Activity(即没有设置setContentView())时,测试成功。 在加载UI文件时,我不知道为什么会出现这种异常。我的gradle设置有什么问题吗?

仅供参考,以下是我的Android Studio gradle文件。

buildscript {
    repositories {
        mavenCentral()
    }

    dependencies {
        classpath 'com.android.tools.build:gradle:0.12.+'
        classpath 'org.robolectric:robolectric-gradle-plugin:0.11.+'
    }
}

allprojects {
    repositories {
        mavenCentral()
    }
}

apply plugin: 'android'
apply plugin: 'robolectric'

android {
    packagingOptions {
        exclude 'LICENSE.txt'
        exclude 'META-INF/LICENSE'
        exclude 'META-INF/LICENSE.txt'
        exclude 'META-INF/NOTICE'
    }
    compileSdkVersion 19
    buildToolsVersion "20.0.0"

    defaultConfig {
        applicationId "com.krossover.coach"
        minSdkVersion 16
        targetSdkVersion 19
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "com.google.android.apps.common.testing.testrunner.GoogleInstrumentationTestRunner"
    }
    buildTypes {
        release {
            runProguard false
        }
    }

    sourceSets {
        androidTest {
            setRoot('src/test')
        }
    }
}

robolectric {
    include '**/*Test.class'
    exclude '**/espresso/**/*.class'
}

dependencies {
    repositories {
        mavenCentral()
    }
    compile 'com.android.support:support-v13:20.+'
    compile 'com.android.support:support-v4:20.+'
    compile 'com.squareup:otto:1.3.5'
    compile 'com.squareup.okhttp:okhttp-urlconnection:2.0.0'
    compile 'com.squareup.okhttp:okhttp:2.0.0'
    compile 'com.squareup.picasso:picasso:2.3.4'
    compile 'com.squareup.retrofit:retrofit:1.6.1'
    compile 'com.jakewharton:butterknife:5.1.2'
    compile 'com.google.android.gms:play-services:5.2.08'
    compile 'com.github.castorflex.smoothprogressbar:library:1.0.0'
    compile 'com.github.castorflex.smoothprogressbar:library-circular:1.0.0'
    compile 'com.android.support:appcompat-v7:20.0.0'

    // Espresso
    androidTestCompile files('lib/espresso-1.1.jar')
    androidTestCompile files('lib/testrunner-1.1.jar')
    androidTestCompile files('lib/testrunner-runtime-1.1.jar')
    androidTestCompile 'com.google.guava:guava:14.0.1'
    androidTestCompile 'com.squareup.dagger:dagger:1.1.0'
    androidTestCompile 'org.hamcrest:hamcrest-integration:1.1'
    androidTestCompile 'org.hamcrest:hamcrest-core:1.1'
    androidTestCompile 'org.hamcrest:hamcrest-library:1.1'
    androidTestCompile('junit:junit:4.11') {
        exclude module: 'hamcrest-core'
    }

    // Roboelectric
    androidTestCompile('org.robolectric:robolectric:2.3') {
        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'
    }
    androidTestCompile 'com.squareup:fest-android:1.0.+'
    compile files('libs/espresso-1.1.jar')
    compile files('libs/testrunner-1.1.jar')
    compile files('libs/testrunner-runtime-1.1.jar')
}

apply plugin: 'idea'

idea {
    module {
        testOutputDir = file('build/test-classes/debug')
    }
}

0 个答案:

没有答案