运行单元测试时,Android Roboletric 3.0-rc2 RuntimeException和NoSuchMethod

时间:2015-05-07 20:09:21

标签: android junit gradle

我只是从Github克隆这个项目:https://github.com/mutexkid/android-studio-robolectric-example

我在Android Studio 1.2,Java 8,Ubuntu Linux发行版上。

使用gradle运行测试时,会生成以下消息。我在Windows操作系统计算机上遇到了同样的错误。

知道我能错过什么?!?

由于

Elcio A。

执行任务:[cleanTest,test]

按需配置是一项孵化功能。 :app:cleanTest UP-TO-DATE :app:preBuild UP-TO-DATE :app:preDebugBuild UP-TO-DATE :应用:checkDebugManifest :应用:prepareDebugDependencies :app:compileDebugAidl UP-TO-DATE :app:compileDebugRenderscript UP-TO-DATE :app:generateDebugBuildConfig UP-TO-DATE :app:generateDebugAssets UP-TO-DATE :app:mergeDebugAssets UP-TO-DATE :app:generateDebugResValues UP-TO-DATE :app:generateDebugResources UP-TO-DATE :app:mergeDebugResources UP-TO-DATE :app:processDebugManifest UP-TO-DATE :app:processDebugResources UP-TO-DATE :app:generateDebugSources UP-TO-DATE :应用:compileDebugJava :应用:preCompileDebugUnitTestJava :应用:compileDebugUnitTestJava :应用:compileDebugUnitTestSources :应用:mockableAndroidJar :应用:assembleDebugUnitTest :应用程序:testDebug

com.example.joshskeen.myapplication.MyActivityTest> clickClickMeButtonChangesHelloWorldText FAILED     了java.lang.RuntimeException         引起:java.lang.RuntimeException             引起:java.lang.NoSuchMethodException

com.example.joshskeen.myapplication.MyActivityTest> myActivityAppearsAsExpectedInitially FAILED     了java.lang.RuntimeException         引起:java.lang.RuntimeException             引起:java.lang.NoSuchMethodException

完成了2次测试,2次失败

:app:testDebug FAILED

失败:构建因异常而失败。

  • 出了什么问题: 任务':app:testDebug'。

    的执行失败
      

    测试失败了。请参阅以下报告:file:///home/elcio/AndroidStudioProjects/android-studio-robolectric-example/app/build/reports/tests/debug/index.html

  • 尝试: 使用--stacktrace选项运行以获取堆栈跟踪。使用--info或--debug选项运行以获得更多日志输出。

建立失败

总时间:10.662秒

这是我的build.grandle:

apply plugin: 'com.android.application'
android {
    compileSdkVersion 22
    buildToolsVersion "22.0.1"

    defaultConfig {
        applicationId "com.example.joshskeen.myapplication"
        minSdkVersion 16
        targetSdkVersion 22
        versionCode 1
        versionName "1.0"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    testCompile 'org.easytesting:fest:1.0.16'
    testCompile 'com.squareup:fest-android:1.0.8'
    testCompile('org.robolectric:robolectric:3.0-rc2') {
        exclude group: 'commons-logging', module: 'commons-logging'
        exclude group: 'org.apache.httpcomponents', module: 'httpclient'
    }
}

这是我的测试类:

package com.example.joshskeen.myapplication;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.Robolectric;
import org.robolectric.RobolectricGradleTestRunner;
import org.robolectric.annotation.Config;

import static org.fest.assertions.api.ANDROID.assertThat;

@RunWith(RobolectricGradleTestRunner.class)
@Config(constants = BuildConfig.class, emulateSdk = 21)
public class MyActivityTest {

    private MyActivity mActivity;

    @Before
    public void setup() {
        mActivity = Robolectric.buildActivity(MyActivity.class).create().get();
    }

    @Test
    public void myActivityAppearsAsExpectedInitially() {
        assertThat(mActivity.mClickMeButton).hasText("Click me!");
        assertThat(mActivity.mHelloWorldTextView).hasText("Hello world!");
    }

    @Test
    public void clickingClickMeButtonChangesHelloWorldText() {
        assertThat(mActivity.mHelloWorldTextView).hasText("Hello world!");
        mActivity.mClickMeButton.performClick();
        assertThat(mActivity.mHelloWorldTextView).hasText("HEY WORLD");
    }
}

0 个答案:

没有答案