【发布时间】:2014-08-21 09:31:34
【问题描述】:
当我尝试执行测试时,我无法使用 Robolectric 执行测试:
./gradlew test
我得到以下异常:
com.Makeupalley.test.HomeRobolectricTest > initializationError FAILED
java.lang.NoClassDefFoundError
Caused by: java.lang.ClassNotFoundException
java.lang.NoClassDefFoundError: android/app/Activity
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:800)
.
.
.
Caused by: java.lang.ClassNotFoundException: android.app.Activity
at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
测试失败,断言如下:assertTrue(true)。
这是我的 build.gradle:
buildscript {
repositories {
mavenCentral()
maven { url 'https://oss.sonatype.org/content/repositories/snapshots/' }
}
dependencies {
classpath 'com.android.tools.build:gradle:0.12.+'
classpath 'org.robolectric:robolectric-gradle-plugin:0.12.+'
}
}
...
dependencies {
compile('junit:junit:4.11') {
exclude module: 'hamcrest-core'
}
compile('org.robolectric:robolectric:2.3') {
exclude module: 'classworlds'
exclude module: 'commons-logging'
exclude module: 'httpclient'
...
}
}
测试文件:
@Config(emulateSdk = 18)
@RunWith(RobolectricTestRunner.class)
public class HomeRobolectricTest {
private TestActivity activity;
public HomeRobolectricTest(){}
@Before
public void setup() {
activity = Robolectric.buildActivity(TestActivity.class).create().get();
}
@Test
public void testInstanceActivity() throws Exception {
assertTrue(true);
}
...
}
有人可以帮我给我一些指导吗?
如果您需要更多信息或所有 build.gradle 文件,我可以发布。
build.gradle 文件:https://gist.github.com/anonymous/d0f0bac71a0fb76f8454
谢谢
更新 - Android 部分
android {
compileSdkVersion 15
buildToolsVersion '19.1.0'
defaultConfig {
minSdkVersion 10
targetSdkVersion 15
versionCode 45
versionName "1.4.210"
testInstrumentationRunner "com.google.android.apps.common.testing.testrunner.GoogleInstrumentationTestRunner"
}
buildTypes {
release {
runProguard true
proguardFile 'proguard-project.txt'
}
debug {
runProguard false
testCoverageEnabled = true
}
}
signingConfigs {
}
packagingOptions {
exclude 'META-INF/INDEX.LIST'
exclude 'META-INF/INDEX'
exclude 'META-INF/LICENSE'
exclude 'META-INF/LICENSE.txt'
exclude 'META-INF/NOTICE'
exclude 'LICENSE.txt'
exclude 'META-INF/license.txt'
exclude 'META-INF/notice.txt'
exclude 'META-INF/ASL2.0'
}
sourceSets {
androidTest {
setRoot('src/test')
}
}
lintOptions {
checkReleaseBuilds false
abortOnError false
}
}
【问题讨论】:
-
我感觉这与说
compile而不是testCompile进行测试有关。看看这个网站,看看是否有帮助:peterfriese.de/android-testing-with-robolectric -
你在 build.gradle 中有 android 部分吗?
-
感谢您的帮助。我使用“androidTestCompile”而不是“compile”并且异常仍然存在。我无法使用“testCompile”,因为我收到以下错误:构建脚本错误,找到了不受支持的 Gradle DSL 方法:'testCompile()'!可能的原因可能是:- 您使用的是没有该方法的 Gradle 版本...
-
我发布了android部分
-
你忘记
apply plugin 'robolectric'了吗?在此之后,您可以将compile更改为testCompile
标签: android gradle android-studio robolectric