gpt4 book ai didi

android - 具有Android注释的Robolectric

转载 作者:行者123 更新时间:2023-12-03 05:17:02 25 4
gpt4 key购买 nike

我在项目中使用android-annotations,然后开始学习单元测试。我正在尝试使用robolectric执行测试,但没有成功(Android Studio 8.6和Gradle)。
通过一些教程,我可以配置build.gradle,如果执行简单的测试而不访问任何 Activity ,则测试会生成OK结果。但是,当我尝试执行访问 Activity 的测试时,会发生错误。

错误

LoginActivityTest > testActivityAA2 FAILED
java.lang.RuntimeException at LoginActivityTest.java:33
Caused by: java.lang.reflect.InvocationTargetException at LoginActivityTest.java:33
Caused by: java.lang.IllegalStateException at LoginActivityTest.java:33

我的测试
@Config(manifest = "./src/main/AndroidManifest.xml", emulateSdk = 18)
@RunWith(RobolectricTestRunner.class)
public class LoginActivityTest extends InstrumentationTestCase {

//This is OK
@Test
public void testNumbers() throws Exception{
final int expected = 5;
final int reality = 5;
assertEquals(expected, reality);
}

// this occurs error
@Test
public void testActivityAA2() throws Exception {
LoginActivity_ loginnActivity = new LoginActivity_();
loginnActivity.onCreate(null);
assertNotNull(loginnActivity);
}
}

我的build.gradle文件
apply plugin: 'com.android.application'
apply plugin: 'android-apt'
def AAVersion = '3.0.1'
apply plugin: 'robolectric'

buildscript {
repositories {
mavenCentral()
}
dependencies {
// replace with the current version of the Android plugin
classpath 'com.android.tools.build:gradle:0.12+'
// the latest version of the android-apt plugin
classpath 'com.neenbedankt.gradle.plugins:android-apt:1.3'
classpath 'org.robolectric:robolectric-gradle-plugin:0.11.+'
}
}

repositories {
mavenCentral()
mavenLocal()
}

dependencies {

apt "org.androidannotations:androidannotations:$AAVersion"
compile "org.androidannotations:androidannotations-api:$AAVersion"

compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:support-v4:20.0.0'
compile 'com.android.support:appcompat-v7:20.0.0'
compile 'com.android.support:gridlayout-v7:20.0.0'
compile 'com.google.code.gson:gson:2.2.4'
compile 'com.readystatesoftware.systembartint:systembartint:1.0.3'
compile 'org.springframework.android:spring-android-rest-template:1.0.1.RELEASE'

// Espresso
androidTestCompile files('lib/espresso-1.1.jar', 'lib/testrunner-1.1.jar', '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'
}
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.+'
}

apply plugin: 'idea'

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

apt {
arguments {
androidManifestFile variant.processResources.manifestFile
resourcePackageName 'com.example.project'

// If you're using Android NBS flavors you should use the following line instead of hard-coded packageName
// resourcePackageName android.defaultConfig.packageName

// You can set optional annotation processing options here, like these commented options:
// logLevel 'INFO'
// logFile '/var/log/aa.log'
}
}

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

android {
compileSdkVersion 19
buildToolsVersion "20.0.0"

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'
}

defaultConfig {
applicationId "br.com.nexxcity.nexxcitypos"
minSdkVersion 7
targetSdkVersion 19
versionCode 1
versionName "1.0"
testInstrumentationRunner "com.google.android.apps.common.testing.testrunner.GoogleInstrumentationTestRunner"
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_7
targetCompatibility JavaVersion.VERSION_1_7
}
buildTypes {
release {
runProguard true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
sourceSets {
androidTest {
setRoot('src/androidTest')
}
}
}

你能帮我吗?
感谢耐心

最佳答案

您不应该像Robolectric测试那样初始化Activity。
使用Robolectric.buildActivity()(文档:http://robolectric.org/activity-lifecycle/)

例如:
Robolectric.buildActivity(LoginActivity.class).create()。get()

根据您需要测试的内容,您可以链接其他方法,例如visible(),start()等。

关于android - 具有Android注释的Robolectric,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25433549/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com