java - Android Studio Robolectric 单元测试 - 默认值无效

标签 java android unit-testing junit robolectric

我想对我的 Android 应用程序使用单元测试。首先,我使用模板项目“deckard-gradle-master”尝试了 Robolectric 的单元测试。由于它运行良好,我想将此测试集成到我现有的 Android 应用程序中。 我添加了库 hamcrest-core-1.3.jar、junit-4.12.jar、robolectric-2.4.jar 和 robolectric-gradle-plugin-1.0.1.jar。 当我尝试在此项目中启动我的 TestClass 时,我总是会遇到以下异常:

Exception in thread "main" java.lang.annotation.AnnotationFormatError: Invalid default: public abstract java.lang.Class org.robolectric.annotation.Config.application()
at java.lang.reflect.Method.getDefaultValue(Method.java:611)
at sun.reflect.annotation.AnnotationType.<init>(AnnotationType.java:128)
at sun.reflect.annotation.AnnotationType.getInstance(AnnotationType.java:85)
at sun.reflect.annotation.AnnotationParser.parseAnnotation2(AnnotationParser.java:266)
at sun.reflect.annotation.AnnotationParser.parseAnnotations2(AnnotationParser.java:120)
at sun.reflect.annotation.AnnotationParser.parseAnnotations(AnnotationParser.java:72)
at java.lang.Class.createAnnotationData(Class.java:3521)
at java.lang.Class.annotationData(Class.java:3510)
at java.lang.Class.getAnnotation(Class.java:3415)
at com.intellij.junit4.JUnit4TestRunnerUtil.buildRequest(JUnit4TestRunnerUtil.java:199)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:39)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:211)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:67)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:134)
Process finished with exit code 1

我真的不知道该怎么办了,但我觉得少了点什么。不幸的是我不知道是什么。

这些是我的 gradle 文件:

// Top-level build file where you can add configuration options common to all sub-projects/modules.

    buildscript {
        repositories {
            jcenter()
        }
        dependencies {
            classpath 'com.android.tools.build:gradle:1.1.0'
            classpath 'org.robolectric:robolectric-gradle-plugin:1.0.1'

            // NOTE: Do not place your application dependencies here; they belong
            // in the individual module build.gradle files
        }
        }

        allprojects {
         repositories {
            jcenter()
        }
        }

apply plugin: 'com.android.application'

android {
    compileSdkVersion 21
    buildToolsVersion "21.1.2"

    defaultConfig {
        applicationId "com.example.bieren.myapplication"
        minSdkVersion 19
        targetSdkVersion 21
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

    dependencies {
        compile fileTree(include: ['*.jar'], dir: 'libs')
        compile 'com.android.support:appcompat-v7:21.0.3'
        compile files('libs/junit-4.12.jar')
        compile files('libs/robolectric-2.4.jar')
        compile files('libs/robolectric-annotations-2.4.jar')
        compile files('libs/hamcrest-core-1.3.jar')
        compile files('libs/android.jar')
        compile files('libs/android-libcore-4.3_r2.robolectric-0.jar')
        compile files('libs/robolectric-2.4-jar-with-dependencies.jar')
        compile files('libs/android-support-v4.jar')
        compile files('libs/maps.jar')
        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.+') {
            exclude module: 'hamcrest-core'
        }
        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: 'support-v4'
            exclude module: 'wagon-file'
            exclude module: 'wagon-http-lightweight'
            exclude module: 'wagon-provider-api'
        }
        compile files('libs/robolectric-gradle-plugin-1.0.1.jar')
    }

这是我的测试类:

package com.example.bieren.testapplication.test;

import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.widget.TextView;

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

import static org.hamcrest.core.IsEqual.equalTo;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;

@RunWith(RobolectricTestRunner.class)
@Config(manifest = "src/main/AndroidManifest.xml", emulateSdk = 18)
public class MainActivityTest {

    private MainActivity activity;

    @Test
    public void testIcon() throws Exception {
        String play_icon = new MainActivity().getResources().getString(R.string.icon_play);
        assertThat(play_icon, equalTo("&#xf144;"));
    }
    @Before
    public void setup(){
        this.activity = Robolectric.buildActivity(MainActivity.class).create().get();
    }
    @Test
    public void buttonClickShouldSetText() throws Exception
    {
        Button button = (Button) activity.findViewById(R.id.button);
        TextView textView = (TextView) activity.findViewById(R.id.nachricht);
        textView.setText("Hans Hans");
        button.performClick();

        assertEquals(textView.getText(),"Hans Hans");
    }

}

如果有人能告诉我问题是什么以及缺少什么,我将非常感激。

丁满

最佳答案

删除 .gradle 文件夹对我来说每次都有效!

如果您没有找到 .gradle 文件夹,这可能是隐藏的。在这种情况下,转到您的项目目录并运行以下命令以删除 .gradle 文件夹。

rm -rf .gradle/

关于java - Android Studio Robolectric 单元测试 - 默认值无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28991106/

相关文章:

java - 如何在 Objective-C 中将整数转换为字节

java - 我可以用什么来在 Java 中设置 cookie?

android - 在 ImageView android 中缩放部分图像

java - Android:如何获取绘制路径的区域大小?

visual-studio-2010 - SQL Server 角色提供程序单元测试期间出现 "The role manager feature has not been enabled"错误

java - 尝试在回收者 View 中实现搜索

java - 遍历 Listview 的子级

android - 如何缩放 View 以适应 Android 动画中的内容大小?

java - Java中的自动内存泄漏检测

android - 错误 : The apk for your currently selected variant (Unknown output) is not signed. 请为此变体指定签名配置(调试)