Java AndroidStudio机器人分子单元测试-Invaild默认值
我想为我的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时,我总是会遇到以下异常:Java AndroidStudio机器人分子单元测试-Invaild默认值,java,android,unit-testing,junit,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时,我总是会遇到以下异常: Exce
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
及
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')
}
这是我的Testclass:
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(""));
}
@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");
}
}
如果有人能告诉我问题出在哪里,遗漏了什么,我真的会非常感激
Timon如果您使用新的测试环境,那么配置Robolectric将更加无缝。看看这个,以便用新的测试环境设置您的项目 作为提醒,您需要将AndroidStudio升级到1.1RC1或更高版本才能正常工作 这种方法避免了对Robolectric使用任何类型的插件。将@Config更改为
@Config(manifest=“app/src/main/AndroidManifest.xml”,emulateSdk=18,reportSdk=18)
为我解决了这个问题。每次删除
.gradle
文件夹对我都有效
如果找不到.gradle
文件夹,可能会将其隐藏。在这种情况下,转到项目目录并运行以下命令以删除.gradle
文件夹
rm -rf .gradle/
你解决了吗?是的。将@Config更改为
@Config(manifest=“app/src/main/AndroidManifest.xml”,emulateSdk=18,reportSdk=18)
为我解决了这个问题。你能发布一个答案以便回答这个问题吗?这对我来说很有效,在多次清理之后,失效并重新启动。。。为什么是2019年,而这似乎仍然是一些愚蠢场景的唯一解决方案。。。
rm -rf .gradle/