当我尝试执行测试时,我无法使用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
}
}
最佳答案
由于android gradle插件版本为1.1.0,因此支持单元测试,因此您可以在gradle文件中使用testCompile。使用设置> gradle>实验性将其打开,并更新gradle插件版本:
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:1.1.0'
}
}