- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在项目中使用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);
}
}
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/
有没有办法在使用 Robolectric 运行测试时修复系统时钟?我有一些代码取决于星期几。在 rspec 中,有 TimeCop gem 允许您为系统修复时间。 Robolectric 有等效项吗?
实际上,我需要获取 API 调用的响应,为此我需要 Context。 最佳答案 更新。 仅用于版本 1.x 和 2.x: Robolectric.application; 对于 3.x 版本: Run
我在使用 roboelectric 2.2 时遇到以下错误 java.lang.IncompatibleClassChangeError: class org.objectweb.asm.tree.C
我正在使用 Robolectric用于我的单元测试并更新了我的Robolectric从 1.2 到 2.2 的 jar 并试图弄清楚如何在这个新版本中绑定(bind)影子类。这是我之前在做的: Rob
将 LeakCanary (1.3) 添加到我的应用程序中: @Override public void onCreate() { super.onCreate(); Fabric.w
我按照官方教程将 robolectric 添加到我现有的 Android 应用程序中。但是当我尝试运行一个简单的测试时,我收到了一个错误: java.lang.RuntimeException: No
我正在为一个使用多 dex 的项目配置 Robolectric。我正在使用 Robolectric 3.0 RC3,它应该支持 Multidex 而不会抛出“Multi dex 安装失败”异常。 te
我是 Robolectric 的新手,请帮助我理解它,这些有什么区别 loginActivity = new LoginActivity();loginActivity = Robolectric.s
在 Android Studio 中同步我的 Gradle 项目时,出现以下错误: Error:Could not find any version that matches org.robolect
我正在尝试使用 Robolectric 设置测试以单击 this repository 中的菜单按钮.基本的 Robolectric 测试将运行,但我无法使用资源运行任何特定于项目的测试,因为它说它找
我在网上搜索了如何使用 android studio 配置 robolectric。 网上有很多资料,但没有一个适合我。每个人都有自己的使用android studio配置Robolectric的步骤
我正在使用 robolectric 来测试一项使用 Google Analytics 的 Activity 。不幸的是,每当我尝试启动 Activity 时,我都会遇到以下异常 android.dat
我按照列出的方式运行迁移 here 运行测试后,我收到带有内部堆栈跟踪的错误 java.lang.RuntimeException: java.lang.ClassNotFoundException:
我在运行测试时出现间歇性故障。 java.lang.NullPointerException at org.robolectric.res.ResBundle$Value.compareTo(
我曾经使用 Android 单元测试运行我的单元测试,但后来切换到使用 Robolectric 的 Java 单元测试。我注意到使用 SaxParser DefaultHandler 的不同之处。基本
我在测试 getView 时遇到错误。测试代码在测试用例期间调用 View.setImageResource(0) 以在异常发生时设置空图像。 我用 Eclipse 运行了测试 robolectric
使用 Robolectric.buildActivity().setup() 的 JUnit 测试在 AndroidX 和 Robolectric 4.3 下不一致地失败。通常在测试套件中,像这样的前
使用.withIntent()创建事件在Robolectric 2中不起作用吗?我正在做以下 activity = Robolectric.buildActivity(MyActivity.c
我知道 Robolectric 官方网站上说这个框架用于单元测试,但在所有示例 robolectric 上都用于测试 View ...例如:Robolectric 可用于检查 textview X 的
是否可以在自己的模块中设置 robolectric 并将其作为依赖项添加到我的项目中?我可以将它添加到我的项目模块中,但如果它在它自己的模块中,我更喜欢它。我创建了一个 javaLibrary 模块并
我是一名优秀的程序员,十分优秀!